Module: Spec::Mocks::ArgumentConstraints
- Included in:
- ExampleMethods
- Defined in:
- lib/gems/rspec-1.1.12/lib/spec/mocks/argument_constraints.rb
Overview
ArgumentConstraints 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 constraints are all positional - they match against the arg in the given position.
Defined Under Namespace
Classes: AnyArgConstraint, AnyArgsConstraint, BooleanConstraint, DuckTypeConstraint, EqualityProxy, HashIncludingConstraint, HashNotIncludingConstraint, InstanceOf, KindOf, MatcherConstraint, NoArgsConstraint, RegexpConstraint
Instance Method Summary collapse
-
#any_args ⇒ Object
:call-seq: object.should_receive(:message).with(any_args()).
-
#anything ⇒ Object
:call-seq: object.should_receive(:message).with(anything()).
-
#boolean ⇒ Object
:call-seq: object.should_receive(:message).with(boolean()).
-
#duck_type(*args) ⇒ Object
:call-seq: object.should_receive(:message).with(duck_type(:hello)) object.should_receive(:message).with(duck_type(:hello, :goodbye)).
-
#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.
-
#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)).
-
#instance_of(klass) ⇒ Object
(also: #an_instance_of)
Passes if arg.instance_of?(klass).
-
#kind_of(klass) ⇒ Object
(also: #a_kind_of)
Passes if arg.kind_of?(klass).
-
#no_args ⇒ Object
:call-seq: object.should_receive(:message).with(no_args).
Instance Method Details
#any_args ⇒ Object
: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)
150 151 152 |
# File 'lib/gems/rspec-1.1.12/lib/spec/mocks/argument_constraints.rb', line 150 def any_args AnyArgsConstraint.new end |
#anything ⇒ Object
:call-seq:
object.should_receive(:message).with(anything())
Passes as long as there is an argument.
158 159 160 |
# File 'lib/gems/rspec-1.1.12/lib/spec/mocks/argument_constraints.rb', line 158 def anything AnyArgConstraint.new(nil) end |
#boolean ⇒ Object
:call-seq:
object.should_receive(:message).with(boolean())
Passes if the argument is boolean.
190 191 192 |
# File 'lib/gems/rspec-1.1.12/lib/spec/mocks/argument_constraints.rb', line 190 def boolean BooleanConstraint.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
182 183 184 |
# File 'lib/gems/rspec-1.1.12/lib/spec/mocks/argument_constraints.rb', line 182 def duck_type(*args) DuckTypeConstraint.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.
200 201 202 |
# File 'lib/gems/rspec-1.1.12/lib/spec/mocks/argument_constraints.rb', line 200 def hash_including(*args) HashIncludingConstraint.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
210 211 212 |
# File 'lib/gems/rspec-1.1.12/lib/spec/mocks/argument_constraints.rb', line 210 def hash_not_including(*args) HashNotIncludingConstraint.new(anythingize_lonely_keys(*args)) end |
#instance_of(klass) ⇒ Object Also known as: an_instance_of
Passes if arg.instance_of?(klass)
215 216 217 |
# File 'lib/gems/rspec-1.1.12/lib/spec/mocks/argument_constraints.rb', line 215 def instance_of(klass) InstanceOf.new(klass) end |
#kind_of(klass) ⇒ Object Also known as: a_kind_of
Passes if arg.kind_of?(klass)
222 223 224 |
# File 'lib/gems/rspec-1.1.12/lib/spec/mocks/argument_constraints.rb', line 222 def kind_of(klass) KindOf.new(klass) end |
#no_args ⇒ Object
:call-seq:
object.should_receive(:message).with(no_args)
Passes if no arguments are passed along with the message
166 167 168 |
# File 'lib/gems/rspec-1.1.12/lib/spec/mocks/argument_constraints.rb', line 166 def no_args NoArgsConstraint.new end |