CARVIEW |
Take the 2008 Git User's Survey and help out! [ hide ]
Every repository with this icon (

Every repository with this icon (

Run the following if you haven't already:
gem sources -a https://gems.github.com
Install the gem(s):
sudo gem install dan-manges-unit_record
Description: | Enables testing ActiveRecord classes without hitting the database. |
Homepage: | https://unit-test-ar.rubyforge.org/ |
Clone URL: |
git://github.com/dan-manges/unit-record.git
Give this clone URL to anyone.
git clone git://github.com/dan-manges/unit-record.git
|
Search Repo: |
name | age | message | |
---|---|---|---|
![]() |
.gitignore | Sun Apr 27 20:47:05 -0700 2008 | add .gitignore, minor readme update [dan-manges] |
![]() |
CHANGELOG | Sun Apr 27 20:47:05 -0700 2008 | add .gitignore, minor readme update [dan-manges] |
![]() |
README.rdoc | Fri Jul 04 13:05:39 -0700 2008 | rename README to README.rdoc for github formatting [dan-manges] |
![]() |
Rakefile | Fri Jul 04 13:05:39 -0700 2008 | rename README to README.rdoc for github formatting [dan-manges] |
![]() |
TODO | Tue Aug 21 21:23:38 -0700 2007 | update docs [dcmanges] |
![]() |
init.rb | Mon Aug 13 19:32:22 -0700 2007 | renaming to unit_record [dcmanges] |
![]() |
lib/ | Fri Feb 08 21:36:46 -0800 2008 | add UnitRecord::AssoicationStubbing to stub ass... [dcmanges] |
![]() |
test/ | Sun Jun 01 15:08:31 -0700 2008 | test with rails 2.1 [dan-manges] |
![]() |
unit_record.gemspec | Fri Jul 04 13:05:39 -0700 2008 | rename README to README.rdoc for github formatting [dan-manges] |
UnitRecord
Enables unit testing ActiveRecord classes without hitting the database.
Why?
Rationale: https://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.[https://github.com/dan-manges/unit-record] Changes are pushed to the Rubyforge git repository.
Thanks
Thanks to Jay Fields for the original implementation.[https://blog.jayfields.com/2007/03/rails-activerecord-unit-testing-part-ii.html]
Contributors
- Dan Manges
- David Lowenfels
- Rob Sanheim
License
Released under Ruby’s[https://www.ruby-lang.org/en/LICENSE.txt] license.[https://www.ruby-lang.org/en/LICENSE.txt]