如何为等待的工作人员建立规范
How to build a spec for a waiting worker
我正在尝试为此调用制定规范(一种简单的工作):
SomeJob.set(wait: 3.seconds).perform_later(messenger.id, EVENT)
我目前拥有的规格:
it 'should call an event for...' do
expect(SomeJob).to receive(:set).with(wait: 3.seconds).and_call_original
subject.save
end
它工作正常,但我还想测试它是否在 3 秒后调用 perform_later
。正确的做法是什么?
谢谢!
您可以使用 ActiveJob::TestHelper
和 ActiveSupport::Testing::TimeHelpers
。
将助手添加到 rails_helper.rb
。
config.include ActiveJob::TestHelper
config.include ActiveSupport::Testing::TimeHelpers
向规范添加测试。
class Some < ApplicationRecord
def hello
SomeJob.set(wait: 3.seconds).perform_later 'Hello!'
end
end
RSpec.describe Some, type: :model do
it 'should start job after 3 seconds' do
time = Time.current
travel_to(time) do
assertion = {
job: SomeJob,
args: ['Hello!'],
at: (time + 3.seconds).to_i
}
assert_enqueued_with(assertion) { Some.new.hello }
end
end
end
我正在尝试为此调用制定规范(一种简单的工作):
SomeJob.set(wait: 3.seconds).perform_later(messenger.id, EVENT)
我目前拥有的规格:
it 'should call an event for...' do
expect(SomeJob).to receive(:set).with(wait: 3.seconds).and_call_original
subject.save
end
它工作正常,但我还想测试它是否在 3 秒后调用 perform_later
。正确的做法是什么?
谢谢!
您可以使用 ActiveJob::TestHelper
和 ActiveSupport::Testing::TimeHelpers
。
将助手添加到 rails_helper.rb
。
config.include ActiveJob::TestHelper
config.include ActiveSupport::Testing::TimeHelpers
向规范添加测试。
class Some < ApplicationRecord
def hello
SomeJob.set(wait: 3.seconds).perform_later 'Hello!'
end
end
RSpec.describe Some, type: :model do
it 'should start job after 3 seconds' do
time = Time.current
travel_to(time) do
assertion = {
job: SomeJob,
args: ['Hello!'],
at: (time + 3.seconds).to_i
}
assert_enqueued_with(assertion) { Some.new.hello }
end
end
end