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
. -
#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(state = nil) ⇒ 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_predicate
. -
#with(*expected_parameters_or_matchers) {|actual_parameters| ... } ⇒ Expectation
Modifies expectation so that the expected method must be called with
expected_parameters_or_matchers
. -
#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 to the block with the specified
parameters
.
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
.
134 135 136 137 |
# File 'lib/mocha/expectation.rb', line 134 def at_least(minimum_number_of_times) @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.
152 153 154 |
# File 'lib/mocha/expectation.rb', line 152 def at_least_once at_least(1) 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
.
170 171 172 173 |
# File 'lib/mocha/expectation.rb', line 170 def at_most(maximum_number_of_times) @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.
188 189 190 |
# File 'lib/mocha/expectation.rb', line 188 def at_most_once at_most(1) 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.
588 589 590 591 |
# File 'lib/mocha/expectation.rb', line 588 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
.
If no block is provided, the method will still attempt to yield resulting in a LocalJumpError
. Note that this is what would happen if a “real” (non-mock) method implementation tried to yield to a non-existent block.
375 376 377 378 |
# File 'lib/mocha/expectation.rb', line 375 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.
114 115 116 117 |
# File 'lib/mocha/expectation.rb', line 114 def never @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.
97 98 99 100 |
# File 'lib/mocha/expectation.rb', line 97 def once @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).
462 463 464 465 |
# File 'lib/mocha/expectation.rb', line 462 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
.
422 423 424 425 |
# File 'lib/mocha/expectation.rb', line 422 def returns(*values) @return_values += ReturnValues.build(*values) self end |
#then ⇒ Expectation #then(state) ⇒ Expectation
Returns the same expectation, thereby allowing invocations of other Mocha::Expectation methods to be chained.
536 537 538 539 |
# File 'lib/mocha/expectation.rb', line 536 def then(state = nil) add_side_effect(ChangeStateSideEffect.new(state)) if state 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).
501 502 503 504 |
# File 'lib/mocha/expectation.rb', line 501 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
.
46 47 48 49 |
# File 'lib/mocha/expectation.rb', line 46 def times(range) @cardinality.times(range) self end |
#twice ⇒ Expectation
Modifies expectation so that the expected method must be called exactly twice.
72 73 74 75 |
# File 'lib/mocha/expectation.rb', line 72 def twice @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_predicate
.
560 561 562 563 |
# File 'lib/mocha/expectation.rb', line 560 def when(state_predicate) add_ordering_constraint(InStateOrderingConstraint.new(state_predicate)) self end |
#with(*expected_parameters_or_matchers) {|actual_parameters| ... } ⇒ Expectation
Modifies expectation so that the expected method must be called with expected_parameters_or_matchers
.
May be used with Ruby literals or variables for exact matching or with parameter matchers for less-specific matching, e.g. ParameterMatchers#includes, ParameterMatchers#has_key, etc. See ParameterMatchers for a list of all available parameter matchers.
Positional arguments were separated from keyword arguments in Ruby v3 (see this article). In relation to this a new configuration option (Configuration#strict_keyword_argument_matching=) is available in Ruby >= 2.7.
When Configuration#strict_keyword_argument_matching= is set to false
(which is currently the default), a positional Hash
and a set of keyword arguments passed to #with are treated the same for the purposes of parameter matching. However, a deprecation warning will be displayed if a positional Hash
matches a set of keyword arguments or vice versa. This is because Configuration#strict_keyword_argument_matching= will default to true
in the future.
When Configuration#strict_keyword_argument_matching= is set to true
, an actual positional Hash
will not match an expected set of keyword arguments; and vice versa, an actual set of keyword arguments will not match an expected positional Hash
, i.e. the parameter matching is stricter.
269 270 271 272 |
# File 'lib/mocha/expectation.rb', line 269 def with(*expected_parameters_or_matchers, &matching_block) @parameters_matcher = ParametersMatcher.new(expected_parameters_or_matchers, self, &matching_block) self end |
#with_block_given ⇒ Expectation
Modifies expectation so that the expected method must be called with a block.
289 290 291 292 |
# File 'lib/mocha/expectation.rb', line 289 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.
308 309 310 311 |
# File 'lib/mocha/expectation.rb', line 308 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 to the block with the specified parameters
.
If no parameters
are specified, it yields to the block without any parameters.
If no block is provided, the method will still attempt to yield resulting in a LocalJumpError
. Note that this is what would happen if a “real” (non-mock) method implementation tried to yield to a non-existent block.
May be called multiple times on the same expectation for consecutive invocations.
347 348 349 |
# File 'lib/mocha/expectation.rb', line 347 def yields(*parameters) multiple_yields(parameters) end |