Class: Matahari::InvocationMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/matahari/invocation_matcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(iterator = nil) ⇒ InvocationMatcher

TODO I can’t work out how to disentangle the 2 responsibilities of this class:

  1. Inspecting and acting on spy invocations

  2. Presenting the results of those inspections.

One to revisit later when my head is less befuddled.



10
11
12
# File 'lib/matahari/invocation_matcher.rb', line 10

def initialize(iterator = nil)
  @expected_call_count = iterator ? iterator.count : nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

Allows chaining of method calls following has_received?/should have_received, e.g. spy.should_have received.some_method, where #some_method is handled by method_missing, its arguments and name being stored until #matches? is called.



37
38
39
40
41
# File 'lib/matahari/invocation_matcher.rb', line 37

def method_missing(sym, *args, &block)
  @call_to_verify = sym
  @args_to_verify = args
  self
end

Instance Method Details

#failure_message_for_shouldObject



26
27
28
29
30
31
32
# File 'lib/matahari/invocation_matcher.rb', line 26

def failure_message_for_should
  if @args_to_verify.size > 0
    "Spy(:#{@subject.name}) expected to receive :#{@call_to_verify}(#{@args_to_verify.map(&:inspect).join(", ")}) #{prettify_times(@expected_call_count)}, received #{prettify_times(@matching_calls)}"
  else
    "Spy(:#{@subject.name}) expected to receive :#{@call_to_verify} #{prettify_times(@expected_call_count)}, received #{prettify_times(@matching_calls)}"
  end
end

#failure_message_for_should_notObject



22
23
24
# File 'lib/matahari/invocation_matcher.rb', line 22

def failure_message_for_should_not
  "Spy(:#{@subject.name}) expected not to receive :#{@call_to_verify} but received it #{prettify_times(@matching_calls)}"
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
# File 'lib/matahari/invocation_matcher.rb', line 14

def matches?(subject)
  @subject = subject
  @invocations_of_method = subject.invocations.select {|i| i.method == @call_to_verify}
  verifying_args = @args_to_verify.size != 0

  match_passes?(verifying_args)
end