我不是要测试rake任务。我有一个测试程序发送电子邮件(真实的电子邮件是)以测试电子邮件模板等。
class EmailTemplatesTest < ActiveSupport::TestCase
context 'send_password_info' do
setup do
Emailtb.send_password_info(user)
Rake::Task['email:run'].invoke # this actually delivers email
end
should 'have one emailtb' do
assert_equal 1, Emailtb.count
end
end
end
当我运行此测试时,我得到以下错误。
RuntimeError: Don't know how to build task 'email:run'
但是,如果我单独运行rake任务,那么它可以正常工作
rake email:run
答案 0 :(得分:3)
测试环境不会在lib中加载文件。您必须在文件顶部手动加载它们,如下所示:
require 'rake'
load File.join(RAILS_ROOT, 'lib', 'tasks', 'my_task.rake')
class EmailTemplatesTest < ActiveSupport::TestCase
context 'send_password_info' do
setup do
Emailtb.send_password_info(user)
Rake::Task['email:run'].invoke # this actually delivers email
end
should 'have one emailtb' do
assert_equal 1, Emailtb.count
end
end
end