Class: Lepus::Testing::RSpecMatchers::PublishLepusMessage
- Inherits:
-
Object
- Object
- Lepus::Testing::RSpecMatchers::PublishLepusMessage
- Defined in:
- lib/lepus/testing/rspec_matchers.rb
Overview
RSpec matcher for testing message publishing
Instance Method Summary collapse
- #description ⇒ Object
- #failure_message ⇒ Object
- #failure_message_when_negated ⇒ Object
-
#initialize(expected_count = nil) ⇒ PublishLepusMessage
constructor
A new instance of PublishLepusMessage.
-
#matches?(actual = nil, &block) ⇒ Boolean
Supports both block expectations and value expectations with a producer class.
-
#supports_block_expectations? ⇒ Boolean
Support block expectations.
-
#to_exchange(exchange_name) ⇒ Object
Chainable methods for specifying expectations.
- #with(payload_matcher) ⇒ Object
- #with_routing_key(routing_key) ⇒ Object
Constructor Details
#initialize(expected_count = nil) ⇒ PublishLepusMessage
Returns a new instance of PublishLepusMessage.
8 9 10 11 12 13 |
# File 'lib/lepus/testing/rspec_matchers.rb', line 8 def initialize(expected_count = nil) @expected_count = expected_count @expected_exchange = nil @expected_routing_key = nil @expected_payload = nil end |
Instance Method Details
#description ⇒ Object
69 70 71 |
# File 'lib/lepus/testing/rspec_matchers.rb', line 69 def description expected_description end |
#failure_message ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/lepus/testing/rspec_matchers.rb', line 43 def = "expected to publish #{expected_description}" += ", but published #{@published_count} message(s)" if @expected_exchange matching_exchange = (@published_count).count { |msg| msg[:exchange] == @expected_exchange } += " to exchange '#{@expected_exchange}' (#{matching_exchange} matched)" end if @expected_routing_key matching_routing = (@published_count).count { |msg| msg[:routing_key] == @expected_routing_key } += " with routing key '#{@expected_routing_key}' (#{matching_routing} matched)" end if @expected_payload matching_payload = (@published_count).count { |msg| @expected_payload.matches?(msg[:payload]) } += " with payload matching #{@expected_payload} (#{matching_payload} matched)" end end |
#failure_message_when_negated ⇒ Object
65 66 67 |
# File 'lib/lepus/testing/rspec_matchers.rb', line 65 def "expected not to publish #{expected_description}, but did" end |
#matches?(actual = nil, &block) ⇒ Boolean
Supports both block expectations and value expectations with a producer class
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/lepus/testing/rspec_matchers.rb', line 21 def matches?(actual = nil, &block) if block || actual.is_a?(Proc) @producer_class = nil @scoped_messages = nil @messages_before = (block || actual).call @messages_after = @published_count = @messages_after - @messages_before elsif actual.is_a?(Class) && actual < Lepus::Producer @producer_class = actual @scoped_messages = Lepus::Testing.(@producer_class) @messages_before = 0 @messages_after = @scoped_messages.size @published_count = @messages_after else return false end matches_count? && matches_exchange? && matches_routing_key? && matches_payload? end |
#supports_block_expectations? ⇒ Boolean
Support block expectations
16 17 18 |
# File 'lib/lepus/testing/rspec_matchers.rb', line 16 def supports_block_expectations? true end |
#to_exchange(exchange_name) ⇒ Object
Chainable methods for specifying expectations
74 75 76 77 |
# File 'lib/lepus/testing/rspec_matchers.rb', line 74 def to_exchange(exchange_name) @expected_exchange = exchange_name.to_s self end |
#with(payload_matcher) ⇒ Object
84 85 86 87 |
# File 'lib/lepus/testing/rspec_matchers.rb', line 84 def with(payload_matcher) @expected_payload = payload_matcher self end |
#with_routing_key(routing_key) ⇒ Object
79 80 81 82 |
# File 'lib/lepus/testing/rspec_matchers.rb', line 79 def with_routing_key(routing_key) @expected_routing_key = routing_key.to_s self end |