Class: RSpec::Mocks::ArgumentListMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/mocks/argument_list_matcher.rb

Overview

Wrapper for matching arguments against a list of expected values. Used by the `with` method on a `MessageExpectation`:

expect(object).to receive(:message).with(:a, 'b', 3)
object.message(:a, 'b', 3)

Values passed to `with` can be literal values or argument matchers that match against the real objects .e.g.

expect(object).to receive(:message).with(hash_including(:a => 'b'))

Can also be used directly to match the contents of any `Array`. This enables 3rd party mocking libs to take advantage of rspec's argument matching without using the rest of rspec-mocks.

require 'rspec/mocks/argument_list_matcher'
include RSpec::Mocks::ArgumentMatchers

arg_list_matcher = RSpec::Mocks::ArgumentListMatcher.new(123, hash_including(:a => 'b'))
arg_list_matcher.args_match?(123, :a => 'b')

This class is immutable.

See Also:

Constant Summary

MATCH_ALL =

Value that will match all argument lists.

new(ArgumentMatchers::AnyArgsMatcher.new)

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (ArgumentListMatcher) initialize(*expected_args)

Initializes an `ArgumentListMatcher` with a collection of literal values and/or argument matchers.

Parameters:

  • expected_args (Array)

    a list of expected literals and/or argument matchers

See Also:



45
46
47
48
49
50
51
52
53
# File 'lib/rspec/mocks/argument_list_matcher.rb', line 45

def initialize(*expected_args)
  @expected_args = expected_args

  @matchers = case expected_args.first
    when ArgumentMatchers::AnyArgsMatcher then Array
    when ArgumentMatchers::NoArgsMatcher  then []
    else expected_args
  end
end

Instance Attribute Details

- (Object) expected_args (readonly)



35
36
37
# File 'lib/rspec/mocks/argument_list_matcher.rb', line 35

def expected_args
  @expected_args
end

Instance Method Details

- (Boolean) args_match?(*args)

Matches each element in the `expected_args` against the element in the same position of the arguments passed to `new`.

Parameters:

  • args (Array)

Returns:

  • (Boolean)

See Also:



62
63
64
# File 'lib/rspec/mocks/argument_list_matcher.rb', line 62

def args_match?(*args)
  Support::FuzzyMatcher.values_match?(@matchers, args)
end