类:RSpec::Mocks::ArgumentListMatcher
- 继承
-
Object
- Object
- RSpec::Mocks::ArgumentListMatcher
- 定义在
- lib/rspec/mocks/argument_list_matcher.rb
概述
用于将参数与预期值列表进行匹配的包装器。由 MessageExpectation
上的 with
方法使用
expect(object).to receive(:message).with(:a, 'b', 3)
object.(:a, 'b', 3)
传递给 with
的值可以是字面值或与实际对象匹配的参数匹配器,例如
expect(object).to receive(:message).with(hash_including(:a => 'b'))
也可以直接用于匹配任何 Array
的内容。这使第三方模拟库能够利用 rspec 的参数匹配,而无需使用 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')
此类是不可变的。
实例方法摘要 折叠
-
#args_match?(*actual_args) ⇒ Boolean
将
expected_args
中的每个元素与传递给new
的参数中相同位置的元素进行匹配。 -
#initialize(*expected_args) ⇒ ArgumentListMatcher 构造函数
使用一组字面值和/或参数匹配器初始化
ArgumentListMatcher
。
构造函数详细信息
#initialize(*expected_args) ⇒ArgumentListMatcher
使用一组字面值和/或参数匹配器初始化 ArgumentListMatcher
。
45 46 47 48 |
# File 'lib/rspec/mocks/argument_list_matcher.rb', line 45 def initialize(*expected_args) @expected_args = expected_args ensure_expected_args_valid! end |
实例方法详细信息
#args_match?(*actual_args) ⇒Boolean
将 expected_args
中的每个元素与传递给 new
的参数中相同位置的元素进行匹配。
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/rspec/mocks/argument_list_matcher.rb', line 58 def args_match?(*actual_args) expected_args = resolve_expected_args_based_on(actual_args) return false if expected_args.size != actual_args.size if RUBY_VERSION >= "3" # If the expectation was set with keywords, while the actual method was called with a positional hash argument, they don't match. # If the expectation was set without keywords, e.g., with({a: 1}), then it fine to call it with either foo(a: 1) or foo({a: 1}). # This corresponds to Ruby semantics, as if the method was def foo(options). if Hash === expected_args.last && Hash === actual_args.last if !Hash.ruby2_keywords_hash?(actual_args.last) && Hash.ruby2_keywords_hash?(expected_args.last) return false end end end Support::FuzzyMatcher.values_match?(expected_args, actual_args) end |