Skip to content

Instantly share code, notes, and snippets.

@denniskuczynski
Created June 29, 2012 14:32
Show Gist options
  • Save denniskuczynski/3018285 to your computer and use it in GitHub Desktop.
Save denniskuczynski/3018285 to your computer and use it in GitHub Desktop.
Test helper for Faking Sidekiq Client Enqueues
##
# Test helper for Faking Sidekiq Client Enqueues
# To use, include in your spec/support directory.
# Sidekiq::Client will be opened and have the fake methods added
#
# To use, call FakeSidekiq.fake! before your test, and
# FakeSidekiq.unfake! after your test. e..g
#
# before(:each) do
# FakeSidekiq.fake!
# end
#
# after(:each) do
# FakeSidekiq.unfake!
# end
#
# Then in your tests, check the values in Sidekiq:Client.fake_queue, e.g.
#
# Sidekiq::Client.fake_queue.size.should eq(1)
# Sidekiq::Client.fake_queue.first[:class].should eq(WorkerClass)
#
##
module Sidekiq
class Client
def self.push_with_fake(item)
fake_queue << item
end
def self.enqueue_with_fake(klass, *args)
fake_queue << { :class => klass, :args => args }
end
def self.fake_queue
@fake_queue ||= []
end
end
end
module FakeSidekiq
def self.fake!
Sidekiq::Client.instance_eval do
class << self
alias_method 'push_without_fake', 'push'
alias_method 'push', 'push_with_fake'
alias_method 'enqueue_without_fake', 'enqueue'
alias_method 'enqueue', 'enqueue_with_fake'
end
end
end
def self.unfake!
Sidekiq::Client.instance_eval do
class << self
alias_method 'push', 'push_without_fake'
alias_method 'enqueue', 'enqueue_without_fake'
end
end
Sidekiq::Client.fake_queue.clear
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment