gpt4 book ai didi

ruby-on-rails - 您如何期望在 Rspec 中使用 .deliver 调用邮件程序函数?

转载 作者:数据小太阳 更新时间:2023-10-29 08:31:13 25 4
gpt4 key购买 nike

我对服务 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?

最佳答案

也对消息传递进行 stub 。按照您最初的期望返回它,然后期望它收到 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

关于ruby-on-rails - 您如何期望在 Rspec 中使用 .deliver 调用邮件程序函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33021092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com