Search code examples
ruby-on-railsrspecsidekiqrspec-sidekiq

Testing that Sidekiq received correct delayed job


Inside of my User model I'm calling a delayed method:

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

And I'm trying to test it like so (using rspec-sidekiq gem):

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

This is what I get:

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"]]

Which basically works, just has a bit different formatting.

How do I fix this test to make sure that Sidekiq received exactly this method with exactly this attribute?


Solution

  • According to the docs, the delay extensions send the entire object in redis, not just the values. If you want to test for that object, you'd need to use YAML.dump(object):

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