【发布时间】:2021-09-26 19:29:20
【问题描述】:
我有这样的工作
class CrawlSsbHistory < BaseJob
retry_on(Ssb::SessionExpired) do
Ssb::Login.call
end
def perform
response = Ssb::Client.get_data
SsbHistory.last.destroy
end
end
我有这样的测试
it "retries the job if session error" do
allow(Ssb::Client).to receive(:get_data).and_raise(Ssb::SessionExpired)
allow(Ssb::Login).to receive(:call)
described_class.perform_now # it is CrawlSsbHistory
expect(Ssb::Login).to have_received(:call)
end
CrawlSsbHistory 是一个抓取一些数据的工作。它调用 Ssb::Client.get_data 来获取数据。
如果会话过期,我会在 Ssb::Client.get_data 内部引发 Ssb::SessionExpired。所以我可以使用 retry_on 捕获作业中引发的错误。那么如果发生了我想试试这份工作。
但我遇到了这样的错误
(Ssb::Login (class)).call(*(any args))
expected: 1 time with any arguments
received: 0 times with any arguments
作业没有调用 retry_on 吗?还是我测试错了?如何制作 rspec 来测试 retry_on 是否正常工作并调用 Ssb::Login.call?
【问题讨论】:
-
是
BaseJobActiveJob::Base? -
是的,它是@LamPhan
标签: ruby-on-rails ruby rspec rspec-rails