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, BooleanMatcher, DuckTypeMatcher, EqualityProxy, HashExcludingMatcher, HashIncludingMatcher, InstanceOf, KindOf, MatcherMatcher, NoArgsMatcher, RegexpMatcher

Instance Method Summary (collapse)

Instance Method Details

- (Object) any_args

Matches any args at all. Supports a more explicit variation of object.should_receive(:message)

Examples:

object.should_receive(:message).with(any_args)
142
143
144
# File 'lib/rspec/mocks/argument_matchers.rb', line 142
def any_args
  AnyArgsMatcher.new
end

- (Object) anything

Matches any argument at all.

Examples:

object.should_receive(:message).with(anything)
151
152
153
# File 'lib/rspec/mocks/argument_matchers.rb', line 151
def anything
  AnyArgMatcher.new(nil)
end

- (Object) boolean

Matches a boolean value.

Examples:

object.should_receive(:message).with(boolean())
179
180
181
# File 'lib/rspec/mocks/argument_matchers.rb', line 179
def boolean
  BooleanMatcher.new(nil)
end

- (Object) duck_type(*args)

Matches if the actual argument responds to the specified messages.

Examples:

object.should_receive(:message).with(duck_type(:hello))
object.should_receive(:message).with(duck_type(:hello, :goodbye))
170
171
172
# File 'lib/rspec/mocks/argument_matchers.rb', line 170
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:

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))
202
203
204
# File 'lib/rspec/mocks/argument_matchers.rb', line 202
def hash_excluding(*args)
  HashExcludingMatcher.new(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:

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))
191
192
193
# File 'lib/rspec/mocks/argument_matchers.rb', line 191
def hash_including(*args)
  HashIncludingMatcher.new(anythingize_lonely_keys(*args))
end

- (Object) instance_of(klass) Also known as: an_instance_of

Matches if arg.instance_of?(klass)

Examples:

object.should_receive(:message).with(instance_of(Thing))
213
214
215
# File 'lib/rspec/mocks/argument_matchers.rb', line 213
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:

object.should_receive(:message).with(kind_of(Thing))
223
224
225
# File 'lib/rspec/mocks/argument_matchers.rb', line 223
def kind_of(klass)
  KindOf.new(klass)
end

- (Object) no_args

Matches no arguments.

Examples:

object.should_receive(:message).with(no_args)
160
161
162
# File 'lib/rspec/mocks/argument_matchers.rb', line 160
def no_args
  NoArgsMatcher.new
end