Rails fast test suite
Rails convention suggests Model classes that traditionally extend ActiveRecord::Base with the corresponding unit tests depending on the database. I prefer to separate the business from the data access layers by having a few ActiveRecord children handling persistence (not unlike repositories) with the bulk of the application logic residing in classes that are unaware of the database.
I find it useful to add a separate test suite for handling the tests for those classes by creating a new directory, e.g.
test/fastunit
and a Rake test task (lib/tasks/fastunit.rake
) that runs the tests in it.
namespace :test do Rake::TestTask.new('fastunit') do |t| t.pattern = 'test/fastunit/*_test.rb' end end Rake::Task[:test].prerequisites
Adding
test:fastunit
to the prerequisites of the main
test
task ensures the suite will be ran as part of the full test build.
One of the advantages to this approach is the instant feedback of running
rake test:fastunit
- these tests are inherently faster. I run these tests often whilst developing to ensure things are going smoothly and only run the full build before checkins.
This technique renders the built-in
test:units
task slightly ambiguous as ActiveRecord tests are now presented more like functional tests, which is not entirely incorrect, because they do, after all, hit the database.