ruby-on-rails - 如何为等待的工人建立规范
问题描述
我正在尝试为此调用制定规范(一种简单的工作):
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
它工作正常,但我也想测试它perform_later
在 3 秒后调用。这样做的正确方法是什么?
谢谢!
解决方案
您可以使用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
推荐阅读
- email - 回复中的模板,转发 (Outlook 2016)
- matlab - 颜色图三种颜色
- arrays - 以编程方式创建嵌套数组
- generics - 使用 kotlin 改造转换器:类型推断失败
- c# - 带有 TextBox 的 CheckedListBox 过滤器
- linux - Bash 脚本 For Loop 包含 if/else 问题
- python - python,字典如何获取第一个键的第一个值
- c++ - 关于 std::hash 实现的公平假设
- javascript - 在 gupshup 中使用 context.sendResponse 时的轮询问题
- sql - 从oracle中的给定字母生成下一个字母序列