Rails / Rspec: How would I test #any? for Sidekiq? -
i have block:
def already_syncing? sidekiq::queue.new(queue_name).any? { |q| q.args[0] == car_id } end
how test this? if stub out this:
allow(sidekiq::queue).to receive_message_chain(:new, :any?) { true }
then condition in actual block not tested.
i think looking and_yield
:
allow(sidekiq::queue).to receive_message_chain(:new, :any?).and_yield(<something>)
here's sample code/specs (with values/method calls didn't know stubbed out) can try , change see fit:
class synctest def already_syncing? sidekiq::queue.new(queue_name).any? { |q| q.args[0] == car_id } end private # values these methods below arbitrarily defined def queue_name 'test' end def car_id 1 end end rspec.describe synctest let(:sync_test) { described_class.new } describe '#already_syncing?' let(:queue) { double('queue') } let(:already_syncing) { sync_test.already_syncing? } before # don't have sidekiq available testing, # it's generic class. stub_const('sidekiq::queue', class.new) allow(sidekiq::queue).to \ receive_message_chain(:new, :any?).and_yield(queue) end context "when queue's args[0] == car_id" before allow(queue).to receive(:args).and_return([1]) end 'returns true' expect(already_syncing).to true end end context "when queue's args[0] != car_id" before allow(queue).to receive(:args).and_return([0]) end 'returns false' expect(already_syncing).to false end end end end
Comments
Post a Comment