Module: Spec::Mocks::ArgumentMatchers

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

Overview

ArgumentMatchers are messages 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(), the matchers are all positional - they match against the arg in the given position.

Defined Under Namespace

Classes: AnyArgMatcher, AnyArgsMatcher, BooleanMatcher, DuckTypeMatcher, EqualityProxy, HashIncludingMatcher, HashNotIncludingMatcher, InstanceOf, KindOf, MatcherMatcher, NoArgsMatcher, RegexpMatcher

Instance Method Summary collapse

Instance Method Details

#any_argsObject

:call-seq:

object.should_receive(:message).with(any_args())

Passes if object receives :message with any args at all. This is really a more explicit variation of object.should_receive(:message)



146
147
148
# File 'lib/spec/mocks/argument_matchers.rb', line 146

def any_args
  AnyArgsMatcher.new
end

#anythingObject

:call-seq:

object.should_receive(:message).with(anything())

Passes as long as there is an argument.



154
155
156
# File 'lib/spec/mocks/argument_matchers.rb', line 154

def anything
  AnyArgMatcher.new(nil)
end

#booleanObject

:call-seq:

object.should_receive(:message).with(boolean())

Passes if the argument is boolean.



186
187
188
# File 'lib/spec/mocks/argument_matchers.rb', line 186

def boolean
  BooleanMatcher.new(nil)
end

#duck_type(*args) ⇒ Object

:call-seq:

object.should_receive(:message).with(duck_type(:hello))
object.should_receive(:message).with(duck_type(:hello, :goodbye))

Passes if the argument responds to the specified messages.

Examples

array = []
display = mock('display')
display.should_receive(:present_names).with(duck_type(:length, :each))
=> passes


178
179
180
# File 'lib/spec/mocks/argument_matchers.rb', line 178

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

#hash_including(*args) ⇒ Object

:call-seq:

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

Passes if the argument is a hash that includes the specified key(s) or key/value pairs. If the hash includes other keys, it will still pass.



196
197
198
# File 'lib/spec/mocks/argument_matchers.rb', line 196

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

#hash_not_including(*args) ⇒ Object

:call-seq:

object.should_receive(:message).with(hash_not_including(:key => val))
object.should_receive(:message).with(hash_not_including(:key))
object.should_receive(:message).with(hash_not_including(:key, :key2 => :val2))

Passes if the argument is a hash that doesn’t include the specified key(s) or key/value



206
207
208
# File 'lib/spec/mocks/argument_matchers.rb', line 206

def hash_not_including(*args)
  HashNotIncludingMatcher.new(anythingize_lonely_keys(*args))
end

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

Passes if arg.instance_of?(klass)



211
212
213
# File 'lib/spec/mocks/argument_matchers.rb', line 211

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

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

Passes if arg.kind_of?(klass)



218
219
220
# File 'lib/spec/mocks/argument_matchers.rb', line 218

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

#no_argsObject

:call-seq:

object.should_receive(:message).with(no_args)

Passes if no arguments are passed along with the message



162
163
164
# File 'lib/spec/mocks/argument_matchers.rb', line 162

def no_args
  NoArgsMatcher.new
end