Class: NotAMock::Matchers::ArgsMatcher

Inherits:
CallMatcher show all
Defined in:
lib/not_a_mock/matchers/args_matcher.rb

Overview

Matcher for

with(...)

Argument Matchers

Not A Mock supports the use of RSpec’s patterns for argument matching in mocks, and extends them. The most useful are listed below.

Anything Matcher

The anything pattern will match any value. For example:

object.should have_received(:message).with(1, anything, 3)

will match the following calls:

object.message(1, 2, 3)
object.message(1, 'Boo!', 3)

but not:

object.message(3, 2, 1)
object.message(1, 2, 3, 4)

In Any Order Matcher

The in_any_order pattern will match an array argument, but won’t care about order of elements in the array. For example:

object.should have_received(:message).with(in_any_order([3, 2, 1]))

will match the following calls:

object.message([3, 2, 1])
object.message([1, 2, 3])

but not:

object.message([1, 2, 3, 4])

Instance Attribute Summary

Attributes inherited from CallMatcher

#calls

Instance Method Summary collapse

Methods inherited from CallMatcher

#and_returned, #exactly, #failure_message, #matched?, #matches?, #negative_failure_message, #once, #twice, #with, #without_args

Constructor Details

#initialize(args, parent = nil) ⇒ ArgsMatcher

Returns a new instance of ArgsMatcher.



46
47
48
49
# File 'lib/not_a_mock/matchers/args_matcher.rb', line 46

def initialize(args, parent = nil)
  super parent
  @args = args
end

Instance Method Details

#failure_message_without_parentsObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/not_a_mock/matchers/args_matcher.rb', line 56

def failure_message_without_parents
  if matched?
    if @args.empty?
      ", without args"
    else
      ", with args #{@args.inspect}"
    end
  else
    if @args.empty?
      ", but not without args"
    else
      ", but not with args #{@args.inspect}"
    end
  end
end

#matches_without_parents?Boolean

Returns:

  • (Boolean)


51
52
53
54
# File 'lib/not_a_mock/matchers/args_matcher.rb', line 51

def matches_without_parents?
  @calls = @parent.calls.select {|entry| @args == entry[:args] }
  !@calls.empty?
end