module RSpec::Mocks::ArgumentMatchers
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
Public Instance Methods
Matches any args at all. Supports a more explicit variation of `object.should_receive(:message)`
@example
object.should_receive(:message).with(any_args)
# File lib/rspec/mocks/argument_matchers.rb, line 142 def any_args AnyArgsMatcher.new end
Matches any argument at all.
@example
object.should_receive(:message).with(anything)
# File lib/rspec/mocks/argument_matchers.rb, line 151 def anything AnyArgMatcher.new(nil) end
Matches a boolean value.
@example
object.should_receive(:message).with(boolean())
# File lib/rspec/mocks/argument_matchers.rb, line 179 def boolean BooleanMatcher.new(nil) end
Matches if the actual argument responds to the specified messages.
@example
object.should_receive(:message).with(duck_type(:hello)) object.should_receive(:message).with(duck_type(:hello, :goodbye))
# File lib/rspec/mocks/argument_matchers.rb, line 170 def duck_type(*args) DuckTypeMatcher.new(*args) end
Matches a hash that doesn't include the specified key(s) or key/value.
@example
object.should_receive(:message).with(hash_excluding(:key => val)) object.should_receive(:message).with(hash_excluding(:key)) object.should_receive(:message).with(hash_excluding(:key, :key2 => :val2))
# File lib/rspec/mocks/argument_matchers.rb, line 202 def hash_excluding(*args) HashExcludingMatcher.new(anythingize_lonely_keys(*args)) end
Matches a hash that includes the specified key(s) or key/value pairs. Ignores any additional keys.
@example
object.should_receive(:message).with(hash_including(:key => val)) object.should_receive(:message).with(hash_including(:key)) object.should_receive(:message).with(hash_including(:key, :key2 => val2))
# File lib/rspec/mocks/argument_matchers.rb, line 191 def hash_including(*args) HashIncludingMatcher.new(anythingize_lonely_keys(*args)) end
Matches if `arg.instance_of?(klass)`
@example
object.should_receive(:message).with(instance_of(Thing))
# File lib/rspec/mocks/argument_matchers.rb, line 213 def instance_of(klass) InstanceOf.new(klass) end
Matches if `arg.kind_of?(klass)` @example
object.should_receive(:message).with(kind_of(Thing))
# File lib/rspec/mocks/argument_matchers.rb, line 223 def kind_of(klass) KindOf.new(klass) end
Matches no arguments.
@example
object.should_receive(:message).with(no_args)
# File lib/rspec/mocks/argument_matchers.rb, line 160 def no_args NoArgsMatcher.new end
Private Instance Methods
# File lib/rspec/mocks/argument_matchers.rb, line 231 def anythingize_lonely_keys(*args) hash = args.last.class == Hash ? args.delete_at(-1) : {} args.each { | arg | hash[arg] = anything } hash end