views:

889

answers:

3

Recently I switched from fixtures to factory_girl to test my Ruby on Rails application. If I run rake test:units, to run the tests in my /units directory, they all run perfectly. The same is true if I run my functional tests (in my /functional directory) with rake test:functionals.

However, if I simply run rake test, to run both my unit and functional tests together, my validation fails on the second group of tests (functional, in this case), with the message "Validation failed: Name has already been taken."

I believe this is caused by the functional tests creating objects with the same parameters as the objects that were created in the unit tests -- leading me to believe that the test database isn't cleared in between the unit and functional tests.

I use factory_girl's sequencing to have unique attributes for objects, which means that factory_girl is being reset between tests, while the database is not. What can I do to solve this problem? Is there a way to clear the database between my two test packages?

+2  A: 

A rails plugin called "override_rake_task" could be used to override Rake task "test" which is defined inside if Rails gem. This is a very simple task that executes 3 other tasks one after another: test:units, test:functionals and test:integration. You could include the execution of "db:test:purge" task to clear the test DB before executing test:functionals.

Apparently if you are not using this plugin and if you define a task in your rails application with the same name, rake would execute both tasks: the default and yours.

Alex Kaushovik
Is there a way to execute db:test:purge between the unit and functional tests without overriding the default rake tasks?
NolanDC
Yes, you could probably do the same thing without a plugin by first removing the default task and adding a new one with the same code + "db:test:purge": "remove_task :test" then "task :test do ... end". I saw this here: http://www.taknado.com/2007/7/30/overriding-rake-tasks/
Alex Kaushovik
+3  A: 

Try writing this in your test/test_helper.rb

eval IO.read(File.expand_path(File.dirname(__FILE__) + "/../Rakefile"))
class Test::Unit::TestCase
 ....
 #db:test:prepare won't work, don't know why,
 #as DROP DATABASE won't execute (me on PostgreSQL).
 #even if I write,
 #ActiveRecord::Base.connection.disconnect!
 Rake::Task["db:reset"].invoke
end

It's not a recommended solution. Makes tests more slower, but works.

Vikrant Chaudhary
Perfect, thanks! I don't have too many tests at this point, so speed isn't a big issue yet.
NolanDC
A: 

The above solutions didn't work for me. If you are trying to reach out to an external database running unit tests can give some weird errors. For some reason they do not get cleared after running the test so you have to run rake db:test:purge after running the unit tests. Put this in your Rakefile and it should fix it.

Rake::Task["db:test:prepare"].enhance do
  Rake::Task["db:test:purge"].invoke
end
jluebbert