I am trying to write a Rspec test for one of my rake task, according to this post by Stephen Hagemann.
lib/tasks/retry.rake
:
namespace :retry do
task :message, [:message_id] => [:environment] do |t, args|
TextMessage.new.resend!(args[:message_id])
end
end
spec/tasks/retry_spec.rb
:
require 'rails_helper'
require 'rake'
describe 'retry namespace rake task' do
describe 'retry:message' do
before do
load File.expand_path("../../../lib/tasks/retry.rake", __FILE__)
Rake::Task.define_task(:environment)
end
it 'should call the resend action on the message with the specified message_id' do
message_id = "5"
expect_any_instance_of(TextMessage).to receive(:resend!).with message_id
Rake::Task["retry:message[#{message_id}]"].invoke
end
end
end
However, when I run this test, I am getting the following error:
Don't know how to build task 'retry:message[5]'
On the other hand, when I run the task with no argument as:
Rake::Task["retry:message"].invoke
I am able to get the rake task invoked, but the test fails as there is no message_id
.
What is wrong with the way I'm passing in the argument into the rake task?
Thanks for all help.