当我在链中间需要更多粒度时,我一直在编写测试instance_doubles
来代替消息链。但是,我想知道我是否正在以艰难的方式做事。
这是我要测试的方法:
def run_produceable_job
# Delete any jobs that exist, but haven't started, in favor of this new job
Delayed::Job.where(queue: 'produceable', locked_at: nil).delete_all
ProduceableJob.perform_later
end
对于Delayed::Job
呼叫,重要的是我检查队列名称是否符合预期。我还想确保 Delayed::Job.delete_all
最后收到
我想做这样的事情:
expect(Delayed::Job).to receive(:where).with(queue: 'produceable', locked_at: nil).and_then_receive(:delete_all)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RSpec 是否为接收提供某种链接?我浏览了文档,但找不到任何专门讨论添加多个接收的内容。
还是我必须走很长的路?
ar_relation = instance_double ActiveRecord::Relation
allow(Delayed::Job).to receive(:where).with(queue: 'produceable', locked_at: nil).and_return(ar_relation)
allow(ar_relation).to receive(:delete_all)
expect(Delayed::Job).to receive(:where).with(queue: 'produceable', locked_at: nil)
expect(ar_relation).to receive(:delete_all)