How to invoke a rake task from within a test program
I am not trying to test rake tasks. I have a test program which sends out emails ( real emails yes) to test email templates etc.
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
When I run this test then I get following error.
RuntimeError: Don't know how to build task 'email:run'
How开发者_StackOverflowever if I run the rake task separately then it works fine
rake email:run
The test environment doesn't load files in lib. You have to manually load them at the top of the file, like so:
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
精彩评论