4

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?

infused
  • 24,000
  • 13
  • 68
  • 78
Serge Vinogradoff
  • 2,262
  • 4
  • 26
  • 42

1 Answers1

7

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)
Peter Brown
  • 50,956
  • 18
  • 113
  • 146