Module: RSpec::Mocks::ArgumentMatchers

Included in:
ExampleMethods
Defined in:
lib/rspec/mocks/argument_matchers.rb

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.

Instance Method Summary collapse

Instance Method Details

#any_argsObject

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)


27
28
29
# File 'lib/rspec/mocks/argument_matchers.rb', line 27

def any_args
  AnyArgsMatcher::INSTANCE
end

#anythingObject

Matches any argument at all.

Examples:


expect(object).to receive(:message).with(anything)


36
37
38
# File 'lib/rspec/mocks/argument_matchers.rb', line 36

def anything
  AnyArgMatcher::INSTANCE
end

#array_including(*args) ⇒ Object

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


87
88
89
90
# File 'lib/rspec/mocks/argument_matchers.rb', line 87

def array_including(*args)
  actually_an_array = Array === args.first && args.count == 1 ? args.first : args
  ArrayIncludingMatcher.new(actually_an_array)
end

#booleanObject

Matches a boolean value.

Examples:


expect(object).to receive(:message).with(boolean())


64
65
66
# File 'lib/rspec/mocks/argument_matchers.rb', line 64

def boolean
  BooleanMatcher::INSTANCE
end

#duck_type(*args) ⇒ Object

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))


55
56
57
# File 'lib/rspec/mocks/argument_matchers.rb', line 55

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

#hash_excluding(*args) ⇒ Object 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))


99
100
101
# File 'lib/rspec/mocks/argument_matchers.rb', line 99

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

#hash_including(*args) ⇒ Object

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))


76
77
78
# File 'lib/rspec/mocks/argument_matchers.rb', line 76

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

#instance_of(klass) ⇒ Object Also known as: an_instance_of

Matches if arg.instance_of?(klass)

Examples:


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


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

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

#kind_of(klass) ⇒ Object Also known as: a_kind_of

Matches if arg.kind_of?(klass)

Examples:


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


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

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

#no_argsObject

Matches no arguments.

Examples:


expect(object).to receive(:message).with(no_args)


45
46
47
# File 'lib/rspec/mocks/argument_matchers.rb', line 45

def no_args
  NoArgsMatcher::INSTANCE
end