Class: Mocha::Expectation
- Inherits:
-
Object
- Object
- Mocha::Expectation
- Defined in:
- lib/mocha/expectation.rb
Overview
Methods on expectations returned from Mock#expects, Mock#stubs, ObjectMethods#expects and ObjectMethods#stubs.
Instance Method Summary collapse
-
#at_least(minimum_number_of_times) ⇒ Expectation
Modifies expectation so that the expected method must be called at least a
minimum_number_of_times
. -
#at_least_once ⇒ Expectation
Modifies expectation so that the expected method must be called at least once.
-
#at_most(maximum_number_of_times) ⇒ Expectation
Modifies expectation so that the expected method must be called at most a
maximum_number_of_times
. -
#at_most_once ⇒ Expectation
Modifies expectation so that the expected method must be called at most once.
-
#in_sequence(sequence, *sequences) ⇒ Expectation
Constrains the expectation so that it must be invoked at the current point in the
sequence
. -
#multiple_yields(*parameter_groups) ⇒ Expectation
Modifies expectation so that when the expected method is called, it yields multiple times per invocation with the specified
parameter_groups
(even if no block is provided, in which case yielding will result in aLocalJumpError
). -
#never ⇒ Expectation
Modifies expectation so that the expected method must never be called.
-
#once ⇒ Expectation
Modifies expectation so that the expected method must be called exactly once.
-
#raises(exception = RuntimeError, message = nil) ⇒ Expectation
Modifies expectation so that when the expected method is called, it raises the specified
exception
with the specifiedmessage
i.e. -
#returns(*values) ⇒ Expectation
Modifies expectation so that when the expected method is called, it returns the specified
value
. -
#then(*parameters) ⇒ Expectation
The same expectation, thereby allowing invocations of other Expectation methods to be chained.
-
#throws(tag, object = nil) ⇒ Expectation
Modifies expectation so that when the expected method is called, it throws the specified
tag
with the specific return valueobject
i.e. -
#times(range) ⇒ Expectation
Modifies expectation so that the number of calls to the expected method must be within a specific
range
. -
#twice ⇒ Expectation
Modifies expectation so that the expected method must be called exactly twice.
-
#when(state_predicate) ⇒ Expectation
Constrains the expectation to occur only when the
state_machine
is in the state specified bystate_name
. -
#with(*expected_parameters) {|actual_parameters| ... } ⇒ Expectation
Modifies expectation so that the expected method must be called with
expected_parameters
. -
#with_block_given ⇒ Expectation
Modifies expectation so that the expected method must be called with a block.
-
#with_no_block_given ⇒ Expectation
Modifies expectation so that the expected method must be called without a block.
-
#yields(*parameters) ⇒ Expectation
Modifies expectation so that when the expected method is called, it yields with the specified
parameters
(even if no block is provided, in which case yielding will result in aLocalJumpError
).
Instance Method Details
#at_least(minimum_number_of_times) ⇒ Expectation
Modifies expectation so that the expected method must be called at least a minimum_number_of_times
.
132 133 134 135 |
# File 'lib/mocha/expectation.rb', line 132 def at_least(minimum_number_of_times) @cardinality = Cardinality.at_least(minimum_number_of_times) self end |
#at_least_once ⇒ Expectation
Modifies expectation so that the expected method must be called at least once.
150 151 152 153 |
# File 'lib/mocha/expectation.rb', line 150 def at_least_once at_least(1) self end |
#at_most(maximum_number_of_times) ⇒ Expectation
Modifies expectation so that the expected method must be called at most a maximum_number_of_times
.
169 170 171 172 |
# File 'lib/mocha/expectation.rb', line 169 def at_most(maximum_number_of_times) @cardinality = Cardinality.at_most(maximum_number_of_times) self end |
#at_most_once ⇒ Expectation
Modifies expectation so that the expected method must be called at most once.
187 188 189 190 |
# File 'lib/mocha/expectation.rb', line 187 def at_most_once at_most(1) self end |
#in_sequence(sequence, *sequences) ⇒ Expectation
Constrains the expectation so that it must be invoked at the current point in the sequence
.
To expect a sequence of invocations, write the expectations in order and add the in_sequence(sequence) clause to each one.
Expectations in a sequence
can have any invocation count.
If an expectation in a sequence is stubbed, rather than expected, it can be skipped in the sequence
.
An expected method can appear in multiple sequences.
539 540 541 542 |
# File 'lib/mocha/expectation.rb', line 539 def in_sequence(sequence, *sequences) sequences.unshift(sequence).each { |seq| add_in_sequence_ordering_constraint(seq) } self end |
#multiple_yields(*parameter_groups) ⇒ Expectation
Modifies expectation so that when the expected method is called, it yields multiple times per invocation with the specified parameter_groups
(even if no block is provided, in which case yielding will result in a LocalJumpError
).
323 324 325 326 |
# File 'lib/mocha/expectation.rb', line 323 def multiple_yields(*parameter_groups) @yield_parameters.add(*parameter_groups) self end |
#never ⇒ Expectation
Modifies expectation so that the expected method must never be called.
112 113 114 115 |
# File 'lib/mocha/expectation.rb', line 112 def never @cardinality = Cardinality.exactly(0) self end |
#once ⇒ Expectation
Modifies expectation so that the expected method must be called exactly once.
Note that this is the default behaviour for an expectation, but you may wish to use it for clarity/emphasis.
95 96 97 98 |
# File 'lib/mocha/expectation.rb', line 95 def once @cardinality = Cardinality.exactly(1) self end |
#raises ⇒ Expectation #raises(exception) ⇒ Expectation #raises(exception, message) ⇒ Expectation
Modifies expectation so that when the expected method is called, it raises the specified exception
with the specified message
i.e. calls Kernel#raise(exception, message).
410 411 412 413 |
# File 'lib/mocha/expectation.rb', line 410 def raises(exception = RuntimeError, = nil) @return_values += ReturnValues.new(ExceptionRaiser.new(exception, )) self end |
#returns(value) ⇒ Expectation #returns(*values) ⇒ Expectation
Modifies expectation so that when the expected method is called, it returns the specified value
.
370 371 372 373 |
# File 'lib/mocha/expectation.rb', line 370 def returns(*values) @return_values += ReturnValues.build(*values) self end |
#then ⇒ Expectation #then(state_machine.is(state_name)) ⇒ Expectation
Returns the same expectation, thereby allowing invocations of other Mocha::Expectation methods to be chained.
484 485 486 487 488 489 490 |
# File 'lib/mocha/expectation.rb', line 484 def then(*parameters) if parameters.length == 1 state = parameters.first add_side_effect(ChangeStateSideEffect.new(state)) end self end |
#throw(tag) ⇒ Expectation #throw(tag, object) ⇒ Expectation
Modifies expectation so that when the expected method is called, it throws the specified tag
with the specific return value object
i.e. calls Kernel#throw(tag, object).
449 450 451 452 |
# File 'lib/mocha/expectation.rb', line 449 def throws(tag, object = nil) @return_values += ReturnValues.new(Thrower.new(tag, object)) self end |
#times(range) ⇒ Expectation
Modifies expectation so that the number of calls to the expected method must be within a specific range
.
44 45 46 47 |
# File 'lib/mocha/expectation.rb', line 44 def times(range) @cardinality = Cardinality.times(range) self end |
#twice ⇒ Expectation
Modifies expectation so that the expected method must be called exactly twice.
70 71 72 73 |
# File 'lib/mocha/expectation.rb', line 70 def twice @cardinality = Cardinality.exactly(2) self end |
#when(state_predicate) ⇒ Expectation
Constrains the expectation to occur only when the state_machine
is in the state specified by state_name
.
511 512 513 514 |
# File 'lib/mocha/expectation.rb', line 511 def when(state_predicate) add_ordering_constraint(InStateOrderingConstraint.new(state_predicate)) self end |
#with(*expected_parameters) {|actual_parameters| ... } ⇒ Expectation
Modifies expectation so that the expected method must be called with expected_parameters
.
May be used with parameter matchers in ParameterMatchers.
223 224 225 226 |
# File 'lib/mocha/expectation.rb', line 223 def with(*expected_parameters, &matching_block) @parameters_matcher = ParametersMatcher.new(expected_parameters, &matching_block) self end |
#with_block_given ⇒ Expectation
Modifies expectation so that the expected method must be called with a block.
242 243 244 245 |
# File 'lib/mocha/expectation.rb', line 242 def with_block_given @block_matcher = BlockMatchers::BlockGiven.new self end |
#with_no_block_given ⇒ Expectation
Modifies expectation so that the expected method must be called without a block.
261 262 263 264 |
# File 'lib/mocha/expectation.rb', line 261 def with_no_block_given @block_matcher = BlockMatchers::NoBlockGiven.new self end |
#yields(*parameters) ⇒ Expectation
Modifies expectation so that when the expected method is called, it yields with the specified parameters
(even if no block is provided, in which case yielding will result in a LocalJumpError
).
May be called multiple times on the same expectation for consecutive invocations.
297 298 299 |
# File 'lib/mocha/expectation.rb', line 297 def yields(*parameters) multiple_yields(parameters) end |