123456789_123456789_123456789_123456789_123456789_

Module: RSpec::Mocks::ArgumentMatchers

Overview

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.

Class Method Summary

Instance Method Summary

Class Method Details

.anythingize_lonely_keys(*args)

This method is for internal use only.
[ GitHub ]

  
# 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

Instance Method Details

#a_kind_of(klass)

Alias for #kind_of.

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/argument_matchers.rb', line 125

alias_method :a_kind_of, :kind_of

#an_instance_of(klass)

Alias for #instance_of.

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/argument_matchers.rb', line 115

alias_method :an_instance_of, :instance_of

#any_args

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

Examples:

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)
[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/argument_matchers.rb', line 26

def any_args
  AnyArgsMatcher::INSTANCE
end

#anything

Matches any argument at all.

Examples:

expect(object).to receive(:message).with(anything)
[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/argument_matchers.rb', line 34

def anything
  AnyArgMatcher::INSTANCE
end

#array_excluding(*args)

Matches an array that excludes the specified items.

Examples:

expect(object).to receive(:message).with(array_excluding(1,2,3))
expect(object).to receive(:message).with(array_excluding([1,2,3]))
[ GitHub ]

  
# 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

#array_including(*args)

Matches an array that includes the specified items at least once. Ignores duplicates and additional values

Examples:

expect(object).to receive(:message).with(array_including(1,2,3))
expect(object).to receive(:message).with(array_including([1,2,3]))
[ GitHub ]

  
# 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

#boolean

Matches a boolean value.

Examples:

expect(object).to receive(:message).with(boolean())
[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/argument_matchers.rb', line 59

def boolean
  BooleanMatcher::INSTANCE
end

#duck_type(*args)

Matches if the actual argument responds to the specified messages.

Examples:

expect(object).to receive(:message).with(duck_type(:hello))
expect(object).to receive(:message).with(duck_type(:hello, :goodbye))
[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/argument_matchers.rb', line 51

def duck_type(*args)
  DuckTypeMatcher.new(*args)
end

#hash_excluding(*args) Also known as: #hash_not_including

Matches a hash that doesn’t include the specified key(s) or key/value.

Examples:

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))
[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/argument_matchers.rb', line 80

def hash_excluding(*args)
  HashExcludingMatcher.new(ArgumentMatchers.anythingize_lonely_keys(*args))
end

#hash_including(*args)

Matches a hash that includes the specified key(s) or key/value pairs. Ignores any additional keys.

Examples:

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))
[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/argument_matchers.rb', line 70

def hash_including(*args)
  HashIncludingMatcher.new(ArgumentMatchers.anythingize_lonely_keys(*args))
end

#hash_not_including(*args)

Alias for #hash_excluding.

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/argument_matchers.rb', line 105

alias_method :hash_not_including, :hash_excluding

#instance_of(klass) Also known as: #an_instance_of

Matches if ‘arg.instance_of?(klass)`

Examples:

expect(object).to receive(:message).with(instance_of(Thing))
[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/argument_matchers.rb', line 111

def instance_of(klass)
  InstanceOf.new(klass)
end

#kind_of(klass) Also known as: #a_kind_of

Matches if ‘arg.kind_of?(klass)`

Examples:

expect(object).to receive(:message).with(kind_of(Thing))
[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/argument_matchers.rb', line 121

def kind_of(klass)
  KindOf.new(klass)
end

#no_args

Matches no arguments.

Examples:

expect(object).to receive(:message).with(no_args)
[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/argument_matchers.rb', line 42

def no_args
  NoArgsMatcher::INSTANCE
end