Take the 2008 Git User's Survey and help out! [ hide ]

public
Rubygem
Description: Enables testing ActiveRecord classes without hitting the database.
Homepage: http://unit-test-ar.rubyforge.org/
Clone URL: git://github.com/dan-manges/unit-record.git
Search Repo:
name age message
folder .gitignore Sun Apr 27 20:47:05 -0700 2008 add .gitignore, minor readme update [dan-manges]
folder CHANGELOG Sun Apr 27 20:47:05 -0700 2008 add .gitignore, minor readme update [dan-manges]
folder README.rdoc Fri Jul 04 13:05:39 -0700 2008 rename README to README.rdoc for github formatting [dan-manges]
folder Rakefile Fri Jul 04 13:05:39 -0700 2008 rename README to README.rdoc for github formatting [dan-manges]
folder TODO Tue Aug 21 21:23:38 -0700 2007 update docs [dcmanges]
folder init.rb Mon Aug 13 19:32:22 -0700 2007 renaming to unit_record [dcmanges]
folder lib/ Fri Feb 08 21:36:46 -0800 2008 add UnitRecord::AssoicationStubbing to stub ass... [dcmanges]
folder test/ Sun Jun 01 15:08:31 -0700 2008 test with rails 2.1 [dan-manges]
folder unit_record.gemspec Fri Jul 04 13:05:39 -0700 2008 rename README to README.rdoc for github formatting [dan-manges]
README.rdoc

UnitRecord

Enables unit testing ActiveRecord classes without hitting the database.

Why?

Rationale: http://www.dcmanges.com/blog/rails-unit-record-test-without-the-database

The biggest benefit to disconnecting unit tests from the database is having a faster test suite. Here is the benchmark from one of my current projects:

  Finished in 19.302702 seconds.
  4920 tests, 7878 assertions, 0 failures, 0 errors

4 seconds per 1,000 tests is a good guideline.

Installation

    gem install unit_record

Usage

Restructuring the Rails Test Directory

The Rails test directory typically places testing for models under test/unit and tests for controllers under test/functional. However, we need to change the definition of unit and functional. Controllers can be unit tested (mocking out models and not rendering the view). Models can be functionally tested (hitting the database). Also, each type of test needs its own test_helper. I recommend restructuring your test directory like this:

  test
    test_helper.rb
    unit
      unit_test_helper.rb
      controllers
      models
    functional
      functional_test_helper.rb
      controllers
      models

You should move existing functional tests into functional/controllers. You will also need to change the require line at the top of those tests to require the functional_test_helper.rb file instead of the test_helper.rb file.

The functional_test_helper.rb file only needs to require test_helper.rb:

  require File.dirname(__FILE__) + "/../test_helper"

For moving unit tests, you have a few options. I recommend moving them to unit/models and then disconnecting your unit tests from the database. Any tests that fail should then be modified to not hit the database or moved to functional/models.

Disconnecting

In the test/unit/unit_test_helper.rb file you created when restructuring your test directory, you should add these lines:

  require File.dirname(__FILE__) + "/../test_helper"
  require "unit_record"
  ActiveRecord::Base.disconnect!

The disconnect! method will do everything necessary to run your unit tests without hitting the database.

Development

Active development occurs on the GitHub.[http://github.com/dan-manges/unit-record] Changes are pushed to the Rubyforge git repository.

Thanks

Thanks to Jay Fields for the original implementation.[http://blog.jayfields.com/2007/03/rails-activerecord-unit-testing-part-ii.html]

Contributors

  • Dan Manges
  • David Lowenfels
  • Rob Sanheim

License

Released under Ruby’s[http://www.ruby-lang.org/en/LICENSE.txt] license.[http://www.ruby-lang.org/en/LICENSE.txt]