测试Sidekiq收到正确的延迟工作

时间:2013-10-21 23:36:38

标签: ruby-on-rails rspec sidekiq rspec-sidekiq

在我的用户模型中,我正在调用延迟方法:

class User < ActiveRecord::Base
  def self.import_items
    ...
    User.delay.keep_only_user_cars(1) # "1" is just for testing
  end
end

我正在尝试测试它(使用rspec-sidekiq gem):

expect(Sidekiq::Extensions::DelayedClass).to have_enqueued_job("keep_only_user_cars", 1)     

这就是我得到的:

Failure/Error: expect(Sidekiq::Extensions::DelayedClass).to have_enqueued_job("keep_only_user_cars", 1)
  expected to have an enqueued Sidekiq::Extensions::DelayedClass job with arguments ["keep_only_user_cars", 1] but none found
  found: [["---\n- !ruby/class 'User'\n- :keep_only_user_cars\n- - 1\n"]]

基本上有效,只是格式有点不同。

如何修复此测试以确保Sidekiq正好使用此属性接收此方法?

1 个答案:

答案 0 :(得分:7)

根据the docs,延迟扩展以redis发送整个对象,而不仅仅是值。如果要测试该对象,则需要使用YAML.dump(object):

expected = YAML.dump([User,:keep_only_user_cars,1])
expect(Sidekiq::Extensions::DelayedClass).to have_enqueued_job(expected)