在 Rails 环境中使用 Rspec 测试 Rake 任务
问题描述:
我正在尝试测试一个 rake 任务,它在其中使用了一个活动记录.
I'm trying to test a rake task and it uses an active record in it.
require 'spec_helper'
require 'rake'
load File.join(Rails.root, 'lib', 'tasks', 'survey.rake')
describe "survey rake tasks" do
describe "survey:send_report" do
it "should send a report" do
Rake::Task['survey:send_report'].invoke
end
end
end
当我运行这个规范 rspec spec/lib/survey_spec.rb
时,我得到这个错误
When I run this spec rspec spec/lib/survey_spec.rb
, I get this error "
RuntimeError:
Don't know how to build task 'environment'
如何通过示例规范在内部加载 :enviroment 任务?
How do I load the :enviroment task inside by example spec?
答
我认为你应该先加载任务:
I think you should first load the tasks:
require 'rake'
MyRailsApp::Application.load_tasks
然后调用您的任务:
Rake::Task['survey:send_report'].invoke