我对服务EmailTrainersAboutMissingReports.call
进行了测试,希望它能够拨打TrainerMonitorMailer.missing_run_reports
:
describe EmailTrainersAboutMissingReports do
describe ('::call') do
context 'missing two group runs' do
let!(:group_run) { FactoryGirl.create :group_run, started_at: 1.day.ago }
let!(:group_run_2) { FactoryGirl.create :group_run, started_at: 1.day.ago }
it 'is successful' do
expect(TrainerMonitorMailer).to receive(:missing_run_reports).with(area,
[group_run, group_run_2])
EmailTrainersAboutMissingReports.call
end
end
end
end
随附服务的代码如下所示:
class EmailTrainersAboutMissingReports
def self.call
response = RunReportMonitor.check_all_got_published_on_time(area)
unless response.success?
TrainerMonitorMailer.missing_run_reports(area, response.group_runs_missing_reports).deliver
end
end
end
我遇到麻烦的地方是我将deliver
方法链接到邮件程序。因此,当我接受测试时,我会收到:
undefined method `deliver' for nil:NilClass
我如何定义期望值,使其期望带有上述参数的missing_run_reports,但是也期望这个链接调用.deliver?
答案 0 :(得分:5)
也存储邮件传递。在最初的期望中返回,然后期望它收到deliver
。像这样:
it 'is successful' do
message_delivery = instance_double(ActionMailer::MessageDelivery)
expect(TrainerMonitorMailer).to receive(:missing_run_reports).
with(area, [group_run, group_run_2]).
and_return(message_delivery)
expect(message_delivery).to receive(:deliver)
EmailTrainersAboutMissingReports.call
end