Class: Downstream::HavePublishedEvent
- Inherits:
-
RSpec::Matchers::BuiltIn::BaseMatcher
- Object
- RSpec::Matchers::BuiltIn::BaseMatcher
- Downstream::HavePublishedEvent
- Defined in:
- lib/downstream/rspec/have_published_event.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#event_class ⇒ Object
readonly
Returns the value of attribute event_class.
Instance Method Summary collapse
- #at_least(count) ⇒ Object
- #at_most(count) ⇒ Object
- #exactly(count) ⇒ Object
- #failure_message ⇒ Object
- #failure_message_when_negated ⇒ Object
-
#initialize(event_class) ⇒ HavePublishedEvent
constructor
A new instance of HavePublishedEvent.
- #matches?(block) ⇒ Boolean
- #once ⇒ Object
- #supports_block_expectations? ⇒ Boolean
- #thrice ⇒ Object
- #times ⇒ Object
- #twice ⇒ Object
- #with(attributes) ⇒ Object
Constructor Details
#initialize(event_class) ⇒ HavePublishedEvent
Returns a new instance of HavePublishedEvent.
7 8 9 10 |
# File 'lib/downstream/rspec/have_published_event.rb', line 7 def initialize(event_class) @event_class = event_class set_expected_number(:exactly, 1) end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
5 6 7 |
# File 'lib/downstream/rspec/have_published_event.rb', line 5 def attributes @attributes end |
#event_class ⇒ Object (readonly)
Returns the value of attribute event_class.
5 6 7 |
# File 'lib/downstream/rspec/have_published_event.rb', line 5 def event_class @event_class end |
Instance Method Details
#at_least(count) ⇒ Object
22 23 24 25 |
# File 'lib/downstream/rspec/have_published_event.rb', line 22 def at_least(count) set_expected_number(:at_least, count) self end |
#at_most(count) ⇒ Object
27 28 29 30 |
# File 'lib/downstream/rspec/have_published_event.rb', line 27 def at_most(count) set_expected_number(:at_most, count) self end |
#exactly(count) ⇒ Object
17 18 19 20 |
# File 'lib/downstream/rspec/have_published_event.rb', line 17 def exactly(count) set_expected_number(:exactly, count) self end |
#failure_message ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/downstream/rspec/have_published_event.rb', line 76 def (+"expected to publish #{event_class.identifier} event").tap do |msg| msg << " #{}, but" if @unmatching_events.any? msg << " published the following events:" @unmatching_events.each do |unmatching_event| msg << "\n #{unmatching_event.inspect}" end else msg << " haven't published anything" end end end |
#failure_message_when_negated ⇒ Object
91 92 93 |
# File 'lib/downstream/rspec/have_published_event.rb', line 91 def "expected not to publish #{event_class.identifier} event" end |
#matches?(block) ⇒ Boolean
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/downstream/rspec/have_published_event.rb', line 52 def matches?(block) raise ArgumentError, "have_published_event only supports block expectations" unless block.is_a?(Proc) events = [] namespace = /^#{Downstream.config.namespace}\./ ActiveSupport::Notifications.subscribed(->(name, event) { events << event }, namespace) do block.call end @matching_events, @unmatching_events = events.partition do |actual_event| (event_class.identifier == actual_event.type) && (attributes.nil? || attributes_match?(actual_event)) end @matching_count = @matching_events.size case @expectation_type when :exactly then @expected_number == @matching_count when :at_most then @expected_number >= @matching_count when :at_least then @expected_number <= @matching_count end end |
#once ⇒ Object
36 37 38 |
# File 'lib/downstream/rspec/have_published_event.rb', line 36 def once exactly(:once) end |
#supports_block_expectations? ⇒ Boolean
48 49 50 |
# File 'lib/downstream/rspec/have_published_event.rb', line 48 def supports_block_expectations? true end |
#thrice ⇒ Object
44 45 46 |
# File 'lib/downstream/rspec/have_published_event.rb', line 44 def thrice exactly(:thrice) end |
#times ⇒ Object
32 33 34 |
# File 'lib/downstream/rspec/have_published_event.rb', line 32 def times self end |
#twice ⇒ Object
40 41 42 |
# File 'lib/downstream/rspec/have_published_event.rb', line 40 def twice exactly(:twice) end |
#with(attributes) ⇒ Object
12 13 14 15 |
# File 'lib/downstream/rspec/have_published_event.rb', line 12 def with(attributes) @attributes = attributes self end |