我试图用rspec测试一个rake任务,为此我需要调用它两次,但它只被调用一次.
it 'first test' do
Rake::Task['my_rake_task'].invoke
# rake task was processed
end
it 'second test' do
Rake::Task['my_rake_task'].invoke
# rake task was NOT processed
end
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用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
Run Code Online (Sandbox Code Playgroud)
在我的rake任务中,我正在使用执行查询ActiveRecord::Base.connection.execute。我确定它在RSepc中具有SQL转换功能。
有任何想法吗?