我在一个文件中有两个 rake 任务。播种和填充。种子正在创建必要的数据,而填充正在为我的测试填充样本数据。
在我的测试中,我正在操作一个数据库(添加新条目、删除它等等)。我正在使用数据库清洁器 gem 来重置数据库但是当我重置数据库时,我需要为下一次测试填充数据。我有以下设置但它不工作数据库仍然是空的并且填充数据不存在。
这是规范/支持/database_cleaner.rb
require 'rake'
load File.expand_path("../../../lib/tasks/my_tasks.rake", __FILE__)
RSpec.configure do |config|
config.use_transactional_fixtures = false
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, :js => true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
Rake::Task.define_task(:environment)
Rake::Task['db:seed'].invoke
Rake::Task['db:populate'].invoke
end
config.after(:each) do
DatabaseCleaner.clean
end
end
规范/rails_helper.rb
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require 'rake'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'factory_girl_rails'
require 'rspec/rails'
require 'capybara/poltergeist'
# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!
Capybara.configure do |config|
config.default_wait_time = 20
end
RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
#
# config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.include FactoryGirl::Syntax::Methods
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.infer_spec_type_from_file_location!
end