Class: Hyrax::Specs::SpyListener

Inherits:
Object
  • Object
show all
Defined in:
lib/hyrax/specs/spy_listener.rb

Overview

A spec support class for assisting in spec testing of the Hyrax::Publisher pub/sub behavior.

For each registered event, there are two corresponding instance methods

  1. ‘#on_<registered_event>`

  2. ‘#<registered_event>`

Then, for any spec you want to make sure Pub/Sub events fire, you can subscribe an instance of Hyrax::Specs::SpyListener. When your spec is completed, unsubscribe the instance.

Examples:

For RSpec, assuming “object.deposited” is a registered event

# Note, based on the assumption that "object.deposited", then the
# listener object will have two corresponding methods:
#
# 1. :on_object_deposited - the method called when we publish an event
# 2. :object_deposited    - the attr_reader that captures and exposed
#                           the event for verification
let(:listener) { Hyrax::Specs::SpyListener.new }
before { Hyrax.publisher.subscribe(listener) }
after  { Hyrax.publisher.unsubscribe(listener) }

it "publishes to the listener" do
  Hyrax::Publisher.instance.publish("object.deposited", object: object, depositor: user)
  expect(listener.object_deposited&.payload).to eq(object: object, depositor: user)
end

See Also: