views:

445

answers:

4

I've written some integration tests that I'd like to run against a copy of my prod database before I push to production. This lets me test all of my routes are still correct, all of the pages render without errors and some of the multipage workflows work as expected.

When I run the integration tests it drops the database I've loaded and loads the test fixtures (as expected). How can I change this behaviour and keep the copy of my production DB I've loaded?

+1  A: 

Integration tests calls db:test:prepare which calls db:test:clone_structure which calls db:structure:dump and db:test:purge

You can write your own task

namespace :your_namespace do
  Rake::TestTask.new(:integration => "db:migrate(if you want") do |t|
    t.libs << "test"
    t.pattern = 'test/integration/**/*_test.rb'
    t.verbose = true
  end
end
aivarsak
See my update above - I'm still seeing the same behaviour with my task I'm afraid
RichH
Strange, it worked for me. Are you running your new task ' rake dbtest:integration'?
aivarsak
Yep - I've also tried renaming 'integration' it to ensure it wasn't behaving oddly with the integration task in the test namespace.
RichH
what 'rake dbtest:integration --trace' says?
aivarsak
Fixed it. I had a fixtures :all in my test_helper.rb . I've created an integration_test_helper now without that line and that's fixed it. Thanks!
RichH
A: 

Setting self.use_transactional_fixtures = true in your integration tests would be useful as well if you don't want to have to reload the production copy between each execution of the test.

Otherwise, the integration test run will splat the data with whatever changes it makes.

madlep
A: 

I needed to add aivarsak's Rake task

namespace :dbtest do  
  Rake::TestTask.new(:integration) do |t|
    t.libs << "test"
    t.pattern = 'test/integration/**/*_test.rb'
    t.verbose = true  
  end
end

and also remove the

fixtures :all

line from the test/test_helper.rb file (or create a new one you reference in your integration test files)

RichH
A: 

To get this to work I had to specifiy the environment when calling the rake task, otherwise it would run the migrations on the development db and then run the tests on the test db; given the example from above

namespace :dbtest do
  Rake::TestTask.new(:integration => "db:migrate") do |t|
    ...

I had to execute the tests like so

rake environment RAILS_ENV=test dbtest:integration
house9