class RSpec::Mocks::ArgumentListMatcher
@see ArgumentMatchers
This class is immutable.
arg_list_matcher.args_match?(123, :a => ‘b’)
arg_list_matcher = RSpec::Mocks::ArgumentListMatcher.new(123, hash_including(:a => ‘b’))
include RSpec::Mocks::ArgumentMatchers
require ‘rspec/mocks/argument_list_matcher’
matching without using the rest of rspec-mocks.
enables 3rd party mocking libs to take advantage of rspec’s argument
Can also be used directly to match the contents of any ‘Array`. This
expect(object).to receive(:message).with(hash_including(:a => ’b’))
match against the real objects .e.g.
Values passed to ‘with` can be literal values or argument matchers that
object.message(:a, ’b’, 3)
expect(object).to receive(:message).with(:a, ‘b’, 3)
the ‘with` method on a `MessageExpectation`:
Wrapper for matching arguments against a list of expected values. Used by
def args_match?(*args)
- See: #initialize -
Parameters:
-
args
(Array
) --
Other tags:
- Api: - public
def args_match?(*args) Support::FuzzyMatcher.values_match?(resolve_expected_args_based_on(args), args) end
def ensure_expected_args_valid!
def ensure_expected_args_valid! if expected_args.count { |a| ArgumentMatchers::AnyArgsMatcher::INSTANCE == a } > 1 raise ArgumentError, "`any_args` can only be passed to " \ "`with` once but you have passed it multiple times." elsif expected_args.count > 1 && expected_args.any? { |a| ArgumentMatchers::NoArgsMatcher::INSTANCE == a } raise ArgumentError, "`no_args` can only be passed as a " \ "singleton argument to `with` (i.e. `with(no_args)`), " \ "but you have passed additional arguments." end end
def initialize(*expected_args)
- See: #args_match? -
See: ArgumentMatchers -
Parameters:
-
expected_args
(Array
) -- a list of expected literals and/or argument matchers
Other tags:
- Api: - public
def initialize(*expected_args) @expected_args = expected_args ensure_expected_args_valid! end
def replace_any_args_with_splat_of_anything(before_count, actual_args_count)
def replace_any_args_with_splat_of_anything(before_count, actual_args_count) any_args_count = actual_args_count - expected_args.count + 1 after_count = expected_args.count - before_count - 1 any_args = 1.upto(any_args_count).map { ArgumentMatchers::AnyArgMatcher::INSTANCE } expected_args.first(before_count) + any_args + expected_args.last(after_count) end
def resolve_expected_args_based_on(actual_args)
- Private: -
def resolve_expected_args_based_on(actual_args) return [] if [ArgumentMatchers::NoArgsMatcher::INSTANCE] == expected_args any_args_index = expected_args.index { |a| ArgumentMatchers::AnyArgsMatcher::INSTANCE == a } return expected_args unless any_args_index replace_any_args_with_splat_of_anything(any_args_index, actual_args.count) end