我正在使用rspec-sidekiq gem (https://github.com/philostler/rspec-sidekiq)来帮助测试我正在编写的一个worker,但是由于某些原因,我的测试总是失败。
这是我的测试:
require 'spec_helper'
describe CommunicationWorker do
it { should be_retryable false }
it "enqueues a communication worker" do
subject.perform("foo@bar.com", "bar@foo.com", [1,2,3])
expect(CommunicationWorker).to have_enqueued_jobs(1)
end
end下面是错误:
1) CommunicationWorker enqueues a communication worker
Failure/Error: expect(CommunicationWorker).to have_enqueued_jobs(1)
expected CommunicationWorker to have 1 enqueued job but got 0
# ./spec/workers/communication_worker_spec.rb:9:in `block (2 levels) in <top (required)>'我在他们的wiki上根据他们的例子进行了低级测试,但这对我不起作用……有没有什么原因会让这个不起作用?
发布于 2013-09-14 02:12:41
这里有两件事需要测试,一个是队列中作业的异步入队,另一个是作业的执行。
您可以通过实例化作业类并调用perform()来测试作业的执行情况。
您可以通过在作业类上调用perform_async()来测试作业的入队情况。
要在测试中测试期望值,您应该执行以下操作:
it "enqueues a communication worker" do
CommunicationWorker.perform_async("foo@bar.com", "bar@foo.com", [1,2,3])
expect(CommunicationWorker).to have(1).jobs
end然而,这实际上只是测试Sidekiq框架,而不是一个有用的测试。我建议为作业本身的内部行为编写测试:
it "enqueues a communication worker" do
Widget.expects(:do_work).with(:some_value)
Mailer.expects(:deliver)
CommunicationWorker.new.perform("foo@bar.com", "bar@foo.com", [1,2,3])
end发布于 2013-12-16 21:40:33
测试方法是什么?尝试用Sidekiq::Testing.fake! do <your code> end包装您现有的测试。这将确保使用假队列。如果sidekiq的测试方法是'inline',那么worker将被立即执行(因此您的队列长度将为0)。
https://stackoverflow.com/questions/18754167
复制相似问题