With RSpec, how to seed the database on load?

后端 未结 6 1431
一个人的身影
一个人的身影 2021-01-30 03:28

I\'m using rspec for testing w my rails 3 app. I need to seed the database before the tests start. How can I seed the database with the following:

/db/seeds.rb

6条回答
  •  爱一瞬间的悲伤
    2021-01-30 03:41

    I ended up needing to use DatabaseCleaner to truncate the database, then load the rake task that does my seeding (because I use seedbank). After that, I wound up wrapping my tests in a transaction like on the database_cleaner README, so that each test could run with a freshly loaded site.

    RSpec.configure do |config|
      config.before(:suite) do
        DatabaseCleaner.strategy = :transaction
        DatabaseCleaner.clean_with(:truncation)
        MyApplicationName::Application.load_tasks
        Rake::Task['db:seed'].invoke # loading seeds
      end
      config.around(:each) do |example|
        DatabaseCleaner.cleaning do
          example.run
        end
      end
    end
    

提交回复
热议问题