[英]Clear database after testing a rake task using RSpec
我正在尝试使用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转换......
有任何想法吗?
你尝试过使用database_cleaner
gem吗?
您可以查看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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.