“rake spec” migrates the database every time

前端 未结 3 1748
北荒
北荒 2021-01-26 11:10

When I run any of the rspec tasks via rake, the database seems to be dropped and migrated, but if I run them via script/spec path/to/spec, it doesn\'t. Is there an option I can

相关标签:
3条回答
  • 2021-01-26 11:29

    It shouldn't be running any migrations, only importing db/schema.rb into your test database. This is the expected behavior so your tests use a fresh copy of the database schema before they run. What is your reasoning for not wanting it to refresh the test database?

    0 讨论(0)
  • 2021-01-26 11:34

    I had this same problem also when running rspec from the command line. In my cases I was working with an legacy database that had no migrations, so the tests would fail because migrations could not be run.

    The solution was to edit the spec/spec_helper.rb file and delete the following line:

    ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
    

    After that the tests ran without failing.

    0 讨论(0)
  • 2021-01-26 11:42

    For what I do I want it off permanently. So with rspec 2.5.0 and rails 3:

    Copy rspec.rake to your apps /lib/tasks folder from:

    ~/.rvm/gems/ruby-1.8.7-p302/gems/rspec-rails-2.5.0/lib/rspec/rails/tasks/rspec.rake
    

    Add this to the top of the file:

      Rake::TaskManager.class_eval do
        def remove_task(task_name)
          @tasks.delete(task_name.to_s)
        end
      end
      def remove_task(task_name) 
        Rake.application.remove_task(task_name) 
      end
      remove_task 'spec'
    

    Find and edit this line to force a noop:

    spec_prereq = :noop #Rails.configuration.generators.options[:rails][:orm] == :active_record ?  "db:test:prepare" : :noop
    
    0 讨论(0)
提交回复
热议问题