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 Class Methods

@private

# File rspec-mocks/lib/rspec/mocks/argument_matchers.rb, line 128
def self.anythingize_lonely_keys(*args)
  hash = Hash === args.last ? args.delete_at(-1) : {}
  args.each { | arg | hash[arg] = AnyArgMatcher::INSTANCE }
  hash
end

Public Instance Methods

Acts like an arg splat, matching any number of args at any point in an arg list.

@example

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)
# File rspec-mocks/lib/rspec/mocks/argument_matchers.rb, line 26
def any_args
  AnyArgsMatcher::INSTANCE
end

Matches any argument at all.

@example

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

Matches an array that excludes the specified items.

@example

expect(object).to receive(:message).with(array_excluding(1,2,3))
expect(object).to receive(:message).with(array_excluding([1,2,3]))
# File rspec-mocks/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

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 rspec-mocks/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

Matches a boolean value.

@example

expect(object).to receive(:message).with(boolean())
# File rspec-mocks/lib/rspec/mocks/argument_matchers.rb, line 59
def boolean
  BooleanMatcher::INSTANCE
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 rspec-mocks/lib/rspec/mocks/argument_matchers.rb, line 51
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 rspec-mocks/lib/rspec/mocks/argument_matchers.rb, line 80
def hash_excluding(*args)
  HashExcludingMatcher.new(ArgumentMatchers.anythingize_lonely_keys(*args))
end
Also aliased as: hash_not_including

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 rspec-mocks/lib/rspec/mocks/argument_matchers.rb, line 70
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 rspec-mocks/lib/rspec/mocks/argument_matchers.rb, line 111
def instance_of(klass)
  InstanceOf.new(klass)
end
Also aliased as: an_instance_of

Matches if ‘arg.kind_of?(klass)`

@example

expect(object).to receive(:message).with(kind_of(Thing))
# File rspec-mocks/lib/rspec/mocks/argument_matchers.rb, line 121
def kind_of(klass)
  KindOf.new(klass)
end
Also aliased as: a_kind_of

Matches no arguments.

@example

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