Skip to content

Instantly share code, notes, and snippets.

@stevo
Created May 7, 2019 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevo/bd11c8dae812c919de6a61d1292dbfe1 to your computer and use it in GitHub Desktop.
Save stevo/bd11c8dae812c919de6a61d1292dbfe1 to your computer and use it in GitHub Desktop.
`subscribe_to` rSpec matcher for use with Wisper
RSpec::Matchers.define :subscribe_to do |event_name|
match do |domain|
@is_registered = Wisper::GlobalListeners.registrations.any? do |registration|
if event_name == :any_event
registration.on.send(:list).nil? &&
(@asynchronously.blank? || registration.broadcaster.is_a?(Wisper::SidekiqBroadcaster))
else
registration.listener == domain &&
registration.on.include?(event_name) &&
(@asynchronously.blank? || registration.broadcaster.is_a?(Wisper::SidekiqBroadcaster))
end
end
return true if @is_registered && event_name == :any_event
if @is_registered
expect(domain).to respond_to(event_name)
handler_class = build_handler_class(event_name, domain)
handler = instance_double(handler_class)
expect(handler_class).to receive(:new).with(a: 10, b: 20) { handler }
expect(handler).to receive(:call!)
domain.public_send(event_name, a: 10, b: 20)
true
end
end
chain :asynchronously do
@asynchronously = true
end
private
def build_handler_class(event_name, domain)
handler_name = event_name.to_s.sub('__', '/').classify
handler_name += 'Handler'
handler_name.remove!('::')
handler_name = handler_name.prepend("#{domain.name}::")
handler_name.constantize
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment