[英]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.