How do I run Rails integration tests without dropping DB contents?

后端 未结 4 1888
故里飘歌
故里飘歌 2021-01-02 04:22

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 corre

相关标签:
4条回答
  • 2021-01-02 04:29

    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
    
    0 讨论(0)
  • 2021-01-02 04:33

    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.

    0 讨论(0)
  • 2021-01-02 04:49

    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
    
    0 讨论(0)
  • 2021-01-02 04:52

    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)

    0 讨论(0)
提交回复
热议问题