Module: ActionSubscriber::RSpec

Defined in:
lib/action_subscriber/rspec.rb

Defined Under Namespace

Classes: FakeChannel

Constant Summary collapse

PROPERTIES_DEFAULTS =
{
  :action => :created,
  :channel => FakeChannel.new,
  :content_type => "text/plain",
  :delivery_tag => "XYZ",
  :exchange => "events",
  :headers => {},
  :message_id => "MSG-123",
  :routing_key => "amigo.user.created",
  :queue => "test.amigo.user.created",
  :uses_acknoledgements => false,
}.freeze

Instance Method Summary collapse

Instance Method Details

#mock_subscriber(opts = {}) ⇒ Object

Create a new subscriber instance. Available options are:

* :acknowledger - the object that should receive ack/reject calls for this message (only useful for testing manual acknowledgment)
* :content_type - defaults to text/plain
* :encoded_payload - the encoded payload object to pass into the instance.
* :exchange - defaults to "events"
* :message_id - defaults to "MSG-123"
* :payload - the payload object to pass to the instance.
* :routing_key - defaults to amigo.user.created
* :subscriber - the class constant corresponding to the subscriber. `described_class` is the default.

Example

describe UserSubscriber do
  subject { mock_subscriber(:payload => proto) }

  it 'logs the user create event' do
    SomeLogger.should_receive(:log)
    subject.created
  end
end


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/action_subscriber/rspec.rb', line 62

def mock_subscriber(opts = {})
  encoded_payload = opts.fetch(:encoded_payload) { double('encoded payload').as_null_object }
  subscriber_class = opts.fetch(:subscriber) { described_class }
  properties = PROPERTIES_DEFAULTS.merge(opts.slice(:channel,
                                                    :content_type,
                                                    :delivery_tag,
                                                    :exchange,
                                                    :message_id,
                                                    :routing_key))


  env = ActionSubscriber::Middleware::Env.new(subscriber_class, encoded_payload, properties)
  env.payload = opts.fetch(:payload) { double('payload').as_null_object }

  return subscriber_class.new(env)
end