我正在尝试使用rspec测试我的rake任务并且运行正常,但问题是之后没有删除记录。
我将config.use_transactional_fixtures = true
放在配置文件中没有任何影响。在其他测试中,它运行良好。
这是我的代码:
require 'rspec'
require 'spec_helper'
require 'rake'
describe 'my_app rake tasks' do
describe 'rake_task_1' do
before { MyApp::Application.load_tasks }
it 'should test it!' do
10.times { create(:record) }
Rake::Task["rake_task_1"].invoke
Record.count.should == 10
end
end
end
在我的rake任务中,我正在使用ActiveRecord::Base.connection.execute
执行查询。
我确信它在RSepc中有一些SQL转换......
任何想法?
答案 0 :(得分:4)
您是否尝试过使用database_cleaner
gem?
您可以查看great article by Avdi Grimm有关如何配置它的信息。
config.use_transactional_fixtures = false
在档案spec/support/database_cleaner.rb
中:
RSpec.configure do |config|
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
end
config.after(:each) do
DatabaseCleaner.clean
end
end