ArgumentMatchers are placeholders that you can include in message expectations to match arguments against a broader check than simple equality.
With the exception of `any_args` and `no_args`, they all match against the arg in same position in the argument list.
@see ArgumentListMatcher
@private
# File lib/rspec/mocks/argument_matchers.rb, line 119 def self.anythingize_lonely_keys(*args) hash = args.last.class == Hash ? args.delete_at(-1) : {} args.each { | arg | hash[arg] = AnyArgMatcher.new } hash end
Matches any args at all. Supports a more explicit variation of `expect(object).to receive(:message)`
@example
expect(object).to receive(:message).with(any_args)
# File lib/rspec/mocks/argument_matchers.rb, line 19 def any_args AnyArgsMatcher.new end
Matches any argument at all.
@example
expect(object).to receive(:message).with(anything)
# File lib/rspec/mocks/argument_matchers.rb, line 28 def anything AnyArgMatcher.new end
Matches an array that includes the specified items at least once. Ignores duplicates and additional values
@example
expect(object).to receive(:message).with(array_including(1,2,3)) expect(object).to receive(:message).with(array_including([1,2,3]))
# File lib/rspec/mocks/argument_matchers.rb, line 79 def array_including(*args) actually_an_array = Array === args.first && args.count == 1 ? args.first : args ArrayIncludingMatcher.new(actually_an_array) end
Matches a boolean value.
@example
expect(object).to receive(:message).with(boolean())
# File lib/rspec/mocks/argument_matchers.rb, line 56 def boolean BooleanMatcher.new end
Matches if the actual argument responds to the specified messages.
@example
expect(object).to receive(:message).with(duck_type(:hello)) expect(object).to receive(:message).with(duck_type(:hello, :goodbye))
# File lib/rspec/mocks/argument_matchers.rb, line 47 def duck_type(*args) DuckTypeMatcher.new(*args) end
Matches a hash that doesn't include the specified key(s) or key/value.
@example
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))
# File lib/rspec/mocks/argument_matchers.rb, line 91 def hash_excluding(*args) HashExcludingMatcher.new(ArgumentMatchers.anythingize_lonely_keys(*args)) end
Matches a hash that includes the specified key(s) or key/value pairs. Ignores any additional keys.
@example
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))
# File lib/rspec/mocks/argument_matchers.rb, line 68 def hash_including(*args) HashIncludingMatcher.new(ArgumentMatchers.anythingize_lonely_keys(*args)) end
Matches if `arg.instance_of?(klass)`
@example
expect(object).to receive(:message).with(instance_of(Thing))
# File lib/rspec/mocks/argument_matchers.rb, line 102 def instance_of(klass) InstanceOf.new(klass) end
Matches if `arg.kind_of?(klass)` @example
expect(object).to receive(:message).with(kind_of(Thing))
# File lib/rspec/mocks/argument_matchers.rb, line 112 def kind_of(klass) klass end
Matches no arguments.
@example
expect(object).to receive(:message).with(no_args)
# File lib/rspec/mocks/argument_matchers.rb, line 37 def no_args NoArgsMatcher.new end