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.

See Also:

Defined Under Namespace

Classes: AnyArgMatcher, AnyArgsMatcher, ArrayIncludingMatcher, BaseHashMatcher, BooleanMatcher, DuckTypeMatcher, HashExcludingMatcher, HashIncludingMatcher, InstanceOf, NoArgsMatcher

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) anythingize_lonely_keys(*args)



119
120
121
122
123
# 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

Instance Method Details

- (Object) any_args

Matches any args at all. Supports a more explicit variation of `expect(object).to receive(:message)`

Examples:


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


19
20
21
# File 'lib/rspec/mocks/argument_matchers.rb', line 19

def any_args
  AnyArgsMatcher.new
end

- (Object) anything

Matches any argument at all.

Examples:


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


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

def anything
  AnyArgMatcher.new
end

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


79
80
81
82
# 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

- (Object) boolean

Matches a boolean value.

Examples:


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


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

def boolean
  BooleanMatcher.new
end

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


47
48
49
# File 'lib/rspec/mocks/argument_matchers.rb', line 47

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

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


91
92
93
# File 'lib/rspec/mocks/argument_matchers.rb', line 91

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

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


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

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

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


102
103
104
# File 'lib/rspec/mocks/argument_matchers.rb', line 102

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

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


112
113
114
# File 'lib/rspec/mocks/argument_matchers.rb', line 112

def kind_of(klass)
  klass
end

- (Object) no_args

Matches no arguments.

Examples:


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


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

def no_args
  NoArgsMatcher.new
end