Class: Mocha::Expectation
Overview
Methods on expectations returned from Mock#expects, Mock#stubs, ObjectMethods#expects and ObjectMethods#stubs.
Instance Attribute Summary collapse
- #backtrace ⇒ Object readonly
Instance Method Summary collapse
- #add_in_sequence_ordering_constraint(sequence) ⇒ Object
- #add_ordering_constraint(ordering_constraint) ⇒ Object
- #add_side_effect(side_effect) ⇒ Object
-
#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_correct_order? ⇒ Boolean
-
#in_sequence(*sequences) ⇒ Expectation
Constrains the expectation so that it must be invoked at the current point in the
sequence
. -
#initialize(mock, expected_method_name, backtrace = nil) ⇒ Expectation
constructor
A new instance of Expectation.
- #invocations_allowed? ⇒ Boolean
- #invoke ⇒ Object
- #match?(actual_method_name, *actual_parameters) ⇒ Boolean
- #matches_method?(method_name) ⇒ Boolean
- #method_signature ⇒ Object
- #mocha_inspect ⇒ Object
-
#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.
- #perform_side_effects ⇒ Object
-
#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
. - #satisfied? ⇒ Boolean
-
#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.
- #used? ⇒ Boolean
- #verified?(assertion_counter = nil) ⇒ Boolean
-
#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
. -
#yields(*parameters) ⇒ Expectation
Modifies expectation so that when the expected method is called, it yields with the specified
parameters
.
Constructor Details
#initialize(mock, expected_method_name, backtrace = nil) ⇒ Expectation
Returns a new instance of Expectation.
503 504 505 506 507 508 509 510 511 512 513 |
# File 'lib/mocha/expectation.rb', line 503 def initialize(mock, expected_method_name, backtrace = nil) @mock = mock @method_matcher = MethodMatcher.new(expected_method_name.to_sym) @parameters_matcher = ParametersMatcher.new @ordering_constraints = [] @side_effects = [] @cardinality, @invocation_count = Cardinality.exactly(1), 0 @return_values = ReturnValues.new @yield_parameters = YieldParameters.new @backtrace = backtrace || caller end |
Instance Attribute Details
#backtrace ⇒ Object (readonly)
500 501 502 |
# File 'lib/mocha/expectation.rb', line 500 def backtrace @backtrace end |
Instance Method Details
#add_in_sequence_ordering_constraint(sequence) ⇒ Object
521 522 523 |
# File 'lib/mocha/expectation.rb', line 521 def add_in_sequence_ordering_constraint(sequence) sequence.constrain_as_next_in_sequence(self) end |
#add_ordering_constraint(ordering_constraint) ⇒ Object
516 517 518 |
# File 'lib/mocha/expectation.rb', line 516 def add_ordering_constraint(ordering_constraint) @ordering_constraints << ordering_constraint end |
#add_side_effect(side_effect) ⇒ Object
526 527 528 |
# File 'lib/mocha/expectation.rb', line 526 def add_side_effect(side_effect) @side_effects << side_effect end |
#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_correct_order? ⇒ Boolean
536 537 538 |
# File 'lib/mocha/expectation.rb', line 536 def in_correct_order? @ordering_constraints.all? { |ordering_constraint| ordering_constraint.allows_invocation_now? } end |
#in_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.
494 495 496 497 |
# File 'lib/mocha/expectation.rb', line 494 def in_sequence(*sequences) sequences.each { |sequence| add_in_sequence_ordering_constraint(sequence) } self end |
#invocations_allowed? ⇒ Boolean
551 552 553 |
# File 'lib/mocha/expectation.rb', line 551 def invocations_allowed? @cardinality.invocations_allowed?(@invocation_count) end |
#invoke ⇒ Object
561 562 563 564 565 566 567 568 569 570 |
# File 'lib/mocha/expectation.rb', line 561 def invoke @invocation_count += 1 perform_side_effects() if block_given? then @yield_parameters.next_invocation.each do |yield_parameters| yield(*yield_parameters) end end @return_values.next end |
#match?(actual_method_name, *actual_parameters) ⇒ Boolean
546 547 548 |
# File 'lib/mocha/expectation.rb', line 546 def match?(actual_method_name, *actual_parameters) @method_matcher.match?(actual_method_name) && @parameters_matcher.match?(actual_parameters) && in_correct_order? end |
#matches_method?(method_name) ⇒ Boolean
541 542 543 |
# File 'lib/mocha/expectation.rb', line 541 def matches_method?(method_name) @method_matcher.match?(method_name) end |
#method_signature ⇒ Object
599 600 601 |
# File 'lib/mocha/expectation.rb', line 599 def method_signature "#{@mock.mocha_inspect}.#{@method_matcher.mocha_inspect}#{@parameters_matcher.mocha_inspect}" end |
#mocha_inspect ⇒ Object
584 585 586 587 588 589 590 591 592 593 594 595 596 |
# File 'lib/mocha/expectation.rb', line 584 def mocha_inspect = "#{@cardinality.mocha_inspect}, " << case @invocation_count when 0 then "not yet invoked" when 1 then "invoked once" when 2 then "invoked twice" else "invoked #{@invocation_count} times" end << ": " << method_signature << "; #{@ordering_constraints.map { |oc| oc.mocha_inspect }.join("; ")}" unless @ordering_constraints.empty? 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
.
279 280 281 282 |
# File 'lib/mocha/expectation.rb', line 279 def multiple_yields(*parameter_groups) @yield_parameters.multiple_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 |
#perform_side_effects ⇒ Object
531 532 533 |
# File 'lib/mocha/expectation.rb', line 531 def perform_side_effects @side_effects.each { |side_effect| side_effect.perform } 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).
366 367 368 369 |
# File 'lib/mocha/expectation.rb', line 366 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
.
326 327 328 329 |
# File 'lib/mocha/expectation.rb', line 326 def returns(*values) @return_values += ReturnValues.build(*values) self end |
#satisfied? ⇒ Boolean
556 557 558 |
# File 'lib/mocha/expectation.rb', line 556 def satisfied? @cardinality.satisfied?(@invocation_count) 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.
440 441 442 443 444 445 446 |
# File 'lib/mocha/expectation.rb', line 440 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).
405 406 407 408 |
# File 'lib/mocha/expectation.rb', line 405 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 |
#used? ⇒ Boolean
579 580 581 |
# File 'lib/mocha/expectation.rb', line 579 def used? @cardinality.used?(@invocation_count) end |
#verified?(assertion_counter = nil) ⇒ Boolean
573 574 575 576 |
# File 'lib/mocha/expectation.rb', line 573 def verified?(assertion_counter = nil) assertion_counter.increment if assertion_counter && @cardinality. @cardinality.verified?(@invocation_count) end |
#when(state_predicate) ⇒ Expectation
Constrains the expectation to occur only when the state_machine
is in the state specified by state_name
.
467 468 469 470 |
# File 'lib/mocha/expectation.rb', line 467 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 |
#yields(*parameters) ⇒ Expectation
Modifies expectation so that when the expected method is called, it yields with the specified parameters
.
May be called multiple times on the same expectation for consecutive invocations.
252 253 254 255 |
# File 'lib/mocha/expectation.rb', line 252 def yields(*parameters) @yield_parameters.add(*parameters) self end |