Class: RSpec::Instrumentation::Matcher::InstrumentMatcher

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

Instance Method Summary collapse

Constructor Details

#initialize(subject) ⇒ InstrumentMatcher

Returns a new instance of InstrumentMatcher.



13
14
15
16
17
18
# File 'lib/rspec-instrumentation-matcher.rb', line 13

def initialize(subject)
  @subject = subject
  @payload = nil
  @received = 0
  @at_least = 1
end

Instance Method Details

#at_least(value) ⇒ Object



66
67
68
69
# File 'lib/rspec-instrumentation-matcher.rb', line 66

def at_least(value)
  @at_least = value
  self
end

#at_most(value) ⇒ Object



71
72
73
74
75
# File 'lib/rspec-instrumentation-matcher.rb', line 71

def at_most(value)
  @at_most = value
  @at_least = nil
  self
end

#descriptionObject



20
21
22
# File 'lib/rspec-instrumentation-matcher.rb', line 20

def description
  "instrument #{@subject} #{expectation_message}"
end

#failure_message(negative = false) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rspec-instrumentation-matcher.rb', line 48

def failure_message(negative = false)
  message = "Expected #{@subject} #{'not ' if negative}to be instrumented"
  message << " at least #{count @at_least}" unless at_least?
  message << " at most #{count @at_most}" unless at_most?
  message << " exactly #{count @times}" unless times?

  if !at_least? || !at_most? || !times?
    message << " but was instrumented #{count @received}"
    message << ' and the payload should have been' unless with?
  else
    message << ' with' unless with?
  end

  message << " #{@payload_expectation.inspect} instead of #{@payload.inspect}" unless with?

  message
end

#matches?(event_proc) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rspec-instrumentation-matcher.rb', line 28

def matches?(event_proc)
  raise_block_syntax_error if block_given?

  callback = lambda do |_name, _start, _finish, _id, payload|
    @payload = payload
    @received += 1
  end
  ActiveSupport::Notifications.subscribed(callback, @subject) do
    event_proc.call
  end

  times? && at_least? && at_most? && with?
end

#negative_failure_messageObject Also known as: failure_message_when_negated



42
43
44
# File 'lib/rspec-instrumentation-matcher.rb', line 42

def negative_failure_message
  failure_message(true)
end

#neverObject



85
86
87
# File 'lib/rspec-instrumentation-matcher.rb', line 85

def never
  times(0)
end

#onceObject



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

def once
  times(1)
end

#supports_block_expectations?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/rspec-instrumentation-matcher.rb', line 24

def supports_block_expectations?
  true
end

#times(value) ⇒ Object



89
90
91
92
93
# File 'lib/rspec-instrumentation-matcher.rb', line 89

def times(value)
  @at_least = nil
  @times = value
  self
end

#twiceObject



81
82
83
# File 'lib/rspec-instrumentation-matcher.rb', line 81

def twice
  times(2)
end

#with(expectation) ⇒ Object



95
96
97
98
99
# File 'lib/rspec-instrumentation-matcher.rb', line 95

def with(expectation)
  @with = true
  @payload_expectation = expectation
  self
end