Class: RSpec::Notifications::Matcher

Inherits:
Object
  • Object
show all
Includes:
Matchers::Composable
Defined in:
lib/rspec/notifications/matcher.rb

Overview

RSpec matcher asserting that a block emits an ActiveSupport::Notifications event. See RSpec::Matchers#emit_notification for the public entry point.

Instance Method Summary collapse

Constructor Details

#initialize(pattern) ⇒ Matcher

Returns a new instance of Matcher.



8
9
10
11
12
13
# File 'lib/rspec/notifications/matcher.rb', line 8

def initialize(pattern)
  @pattern = pattern
  @expected_payload = nil
  @count_type = nil
  @count = nil
end

Instance Method Details

#at_least(count) ⇒ Object



34
35
36
# File 'lib/rspec/notifications/matcher.rb', line 34

def at_least(count)
  set_count(:at_least, count)
end

#at_most(count) ⇒ Object



38
39
40
# File 'lib/rspec/notifications/matcher.rb', line 38

def at_most(count)
  set_count(:at_most, count)
end

#descriptionObject



66
67
68
69
70
71
# File 'lib/rspec/notifications/matcher.rb', line 66

def description
  desc = "emit #{@pattern.inspect} notification"
  desc += " with payload #{description_of(@expected_payload)}" if @expected_payload
  desc += " #{count_description}" if @count_type
  desc
end

#does_not_match?(block) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/rspec/notifications/matcher.rb', line 58

def does_not_match?(block)
  !matches?(block)
end

#exactly(count) ⇒ Object



30
31
32
# File 'lib/rspec/notifications/matcher.rb', line 30

def exactly(count)
  set_count(:exactly, count)
end

#failure_messageObject



73
74
75
# File 'lib/rspec/notifications/matcher.rb', line 73

def failure_message
  "expected block to #{description}, but #{observed_summary}#{emitted_breakdown}"
end

#failure_message_when_negatedObject



77
78
79
# File 'lib/rspec/notifications/matcher.rb', line 77

def failure_message_when_negated
  "expected block not to #{description}, but #{observed_summary}#{emitted_breakdown}"
end

#matches?(block) ⇒ Boolean

— matcher protocol ———————————————-

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
# File 'lib/rspec/notifications/matcher.rb', line 49

def matches?(block)
  @events = Subscriber.capture(subscribe_pattern, &block)
  @matching_events = @events.select do |event|
    PayloadMatcher.matches?(@expected_payload, event.payload)
  end

  count_satisfied?(@matching_events.size)
end

#onceObject



22
23
24
# File 'lib/rspec/notifications/matcher.rb', line 22

def once
  exactly(1)
end

#supports_block_expectations?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/rspec/notifications/matcher.rb', line 62

def supports_block_expectations?
  true
end

#timesObject

syntactic sugar: exactly(n).times



43
44
45
# File 'lib/rspec/notifications/matcher.rb', line 43

def times
  self
end

#twiceObject



26
27
28
# File 'lib/rspec/notifications/matcher.rb', line 26

def twice
  exactly(2)
end

#with(payload) ⇒ Object

— chained expectations ——————————————



17
18
19
20
# File 'lib/rspec/notifications/matcher.rb', line 17

def with(payload)
  @expected_payload = payload
  self
end