模块:RSpec::Mocks::ArgumentMatchers

包含于
ExampleMethods
定义于
lib/rspec/mocks/argument_matchers.rb

概述

ArgumentMatchers 是占位符,您可以在消息预期中包含它们,以针对比简单相等性更广泛的检查匹配参数。

除了 any_argsno_args 之外,它们都与参数列表中相同位置的 arg 匹配。

另请参见

实例方法摘要 折叠

实例方法详情

#any_argsObject

类似于 arg splat,匹配参数列表中任何位置的任意数量的 arg。

示例

expect(object).to receive(:message).with(1, 2, any_args)
# matches any of these:
object.message(1, 2)
object.message(1, 2, 3)
object.message(1, 2, 3, 4)
26
27
28
# File 'lib/rspec/mocks/argument_matchers.rb', line 26
def any_args
  AnyArgsMatcher::INSTANCE
end

#anythingObject

匹配任何参数。

示例

expect(object).to receive(:message).with(anything)
34
35
36
# File 'lib/rspec/mocks/argument_matchers.rb', line 34
def anything
  AnyArgMatcher::INSTANCE
end

#array_excluding(*args) ⇒Object

匹配不包含指定项的数组。

示例

expect(object).to receive(:message).with(array_excluding(1,2,3))
expect(object).to receive(:message).with(array_excluding([1,2,3]))
100
101
102
103
# File 'lib/rspec/mocks/argument_matchers.rb', line 100
def array_excluding(*args)
  actually_an_array = Array === args.first && args.count == 1 ? args.first : args
  ArrayExcludingMatcher.new(actually_an_array)
end

#array_including(*args) ⇒Object

匹配至少包含一次指定项的数组。忽略重复项和附加值

示例

expect(object).to receive(:message).with(array_including(1,2,3))
expect(object).to receive(:message).with(array_including([1,2,3]))
90
91
92
93
# File 'lib/rspec/mocks/argument_matchers.rb', line 90
def array_including(*args)
  actually_an_array = Array === args.first && args.count == 1 ? args.first : args
  ArrayIncludingMatcher.new(actually_an_array)
end

#booleanObject

匹配布尔值。

示例

expect(object).to receive(:message).with(boolean())
59
60
61
# File 'lib/rspec/mocks/argument_matchers.rb', line 59
def boolean
  BooleanMatcher::INSTANCE
end

#duck_type(*args) ⇒Object

如果实际参数响应指定消息,则匹配。

示例

expect(object).to receive(:message).with(duck_type(:hello))
expect(object).to receive(:message).with(duck_type(:hello, :goodbye))
51
52
53
# File 'lib/rspec/mocks/argument_matchers.rb', line 51
def duck_type(*args)
  DuckTypeMatcher.new(*args)
end

#hash_excluding(*args) ⇒Object 也称为:hash_not_including

匹配不包含指定键(s)或键/值对的哈希表。

示例

expect(object).to receive(:message).with(hash_excluding(:key => val))
expect(object).to receive(:message).with(hash_excluding(:key))
expect(object).to receive(:message).with(hash_excluding(:key, :key2 => :val2))
80
81
82
# File 'lib/rspec/mocks/argument_matchers.rb', line 80
def hash_excluding(*args)
  HashExcludingMatcher.new(ArgumentMatchers.anythingize_lonely_keys(*args))
end

#hash_including(*args) ⇒Object

匹配包含指定键(s)或键/值对的哈希表。忽略任何其他键。

示例

expect(object).to receive(:message).with(hash_including(:key => val))
expect(object).to receive(:message).with(hash_including(:key))
expect(object).to receive(:message).with(hash_including(:key, :key2 => val2))
70
71
72
# File 'lib/rspec/mocks/argument_matchers.rb', line 70
def hash_including(*args)
  HashIncludingMatcher.new(ArgumentMatchers.anythingize_lonely_keys(*args))
end

#instance_of(klass) ⇒Object 也称为:an_instance_of

如果 arg.instance_of?(klass),则匹配

示例

expect(object).to receive(:message).with(instance_of(Thing))
111
112
113
# File 'lib/rspec/mocks/argument_matchers.rb', line 111
def instance_of(klass)
  InstanceOf.new(klass)
end

#kind_of(klass) ⇒Object 也称为:a_kind_of

如果 arg.kind_of?(klass),则匹配

示例

expect(object).to receive(:message).with(kind_of(Thing))
121
122
123
# File 'lib/rspec/mocks/argument_matchers.rb', line 121
def kind_of(klass)
  KindOf.new(klass)
end

#no_argsObject

匹配无参数。

示例

expect(object).to receive(:message).with(no_args)
42
43
44
# File 'lib/rspec/mocks/argument_matchers.rb', line 42
def no_args
  NoArgsMatcher::INSTANCE
end