diff --git a/lib/mocha/cardinality.rb b/lib/mocha/cardinality.rb index 6cc00684d..6a69e662c 100644 --- a/lib/mocha/cardinality.rb +++ b/lib/mocha/cardinality.rb @@ -26,37 +26,42 @@ def times(range_or_count) def initialize(required, maximum) @required = required @maximum = maximum + @invocations = [] end - def invocations_allowed?(invocation_count) - invocation_count < maximum + def <<(invocation) + @invocations << invocation end - def satisfied?(invocations_so_far) - invocations_so_far >= required + def invocations_allowed? + @invocations.size < maximum + end + + def satisfied? + @invocations.size >= required end def needs_verifying? !allowed_any_number_of_times? end - def verified?(invocation_count) - (invocation_count >= required) && (invocation_count <= maximum) + def verified? + (@invocations.size >= required) && (@invocations.size <= maximum) end def allowed_any_number_of_times? required.zero? && infinite?(maximum) end - def used?(invocation_count) - (invocation_count > 0) || maximum.zero? + def used? + @invocations.any? || maximum.zero? end - def mocha_inspect + def anticipated_times if allowed_any_number_of_times? 'allowed any number of times' elsif required.zero? && maximum.zero? - 'expected never' + "expected #{times(maximum)}" elsif required == maximum "expected exactly #{times(required)}" elsif infinite?(maximum) @@ -68,13 +73,21 @@ def mocha_inspect end end + def invoked_times + "invoked #{times(@invocations.size)}" + end + + def actual_invocations + @invocations.map(&:mocha_inspect).join + end + protected attr_reader :required, :maximum def times(number) case number - when 0 then 'no times' + when 0 then 'never' when 1 then 'once' when 2 then 'twice' else "#{number} times" diff --git a/lib/mocha/exception_raiser.rb b/lib/mocha/exception_raiser.rb index 47f43a577..8b2430dfb 100644 --- a/lib/mocha/exception_raiser.rb +++ b/lib/mocha/exception_raiser.rb @@ -5,7 +5,8 @@ def initialize(exception, message) @message = message end - def evaluate + def evaluate(invocation) + invocation.raised(@exception) raise @exception, @exception.to_s if @exception.is_a?(Module) && (@exception < Interrupt) raise @exception, @message if @message raise @exception diff --git a/lib/mocha/expectation.rb b/lib/mocha/expectation.rb index 638db7d45..4256c62fa 100644 --- a/lib/mocha/expectation.rb +++ b/lib/mocha/expectation.rb @@ -9,6 +9,7 @@ require 'mocha/in_state_ordering_constraint' require 'mocha/change_state_side_effect' require 'mocha/cardinality' +require 'mocha/invocation' module Mocha # Methods on expectations returned from {Mock#expects}, {Mock#stubs}, {ObjectMethods#expects} and {ObjectMethods#stubs}. @@ -512,7 +513,6 @@ def initialize(mock, expected_method_name, backtrace = nil) @ordering_constraints = [] @side_effects = [] @cardinality = Cardinality.exactly(1) - @invocation_count = 0 @return_values = ReturnValues.new @yield_parameters = YieldParameters.new @backtrace = backtrace || caller @@ -555,33 +555,31 @@ def match?(actual_method_name, *actual_parameters) # @private def invocations_allowed? - @cardinality.invocations_allowed?(@invocation_count) + @cardinality.invocations_allowed? end # @private def satisfied? - @cardinality.satisfied?(@invocation_count) + @cardinality.satisfied? end # @private - def invoke - @invocation_count += 1 + def invoke(*arguments) perform_side_effects - @yield_parameters.next_invocation.each do |yield_parameters| - yield(*yield_parameters) - end - @return_values.next + invocation = Invocation.new(method_name, @yield_parameters, @return_values) + @cardinality << invocation + invocation.call(*arguments) { |*yield_args| yield(*yield_args) } end # @private def verified?(assertion_counter = nil) assertion_counter.increment if assertion_counter && @cardinality.needs_verifying? - @cardinality.verified?(@invocation_count) + @cardinality.verified? end # @private def used? - @cardinality.used?(@invocation_count) + @cardinality.used? end # @private @@ -593,22 +591,21 @@ def inspect # @private def mocha_inspect - message = "#{@cardinality.mocha_inspect}, " - message << 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 - message << ': ' - message << method_signature + message = "#{@cardinality.anticipated_times}, #{@cardinality.invoked_times}: #{method_signature}" message << "; #{@ordering_constraints.map(&:mocha_inspect).join('; ')}" unless @ordering_constraints.empty? + message << @cardinality.actual_invocations if (ENV['MOCHA_OPTIONS'] || '').split(',').include?('verbose') message end # @private def method_signature - "#{@mock.mocha_inspect}.#{@method_matcher.mocha_inspect}#{@parameters_matcher.mocha_inspect}" + "#{method_name}#{@parameters_matcher.mocha_inspect}" + end + + private + + def method_name + "#{@mock.mocha_inspect}.#{@method_matcher.mocha_inspect}" end end end diff --git a/lib/mocha/invocation.rb b/lib/mocha/invocation.rb new file mode 100644 index 000000000..84d86c006 --- /dev/null +++ b/lib/mocha/invocation.rb @@ -0,0 +1,50 @@ +require 'mocha/parameters_matcher' +require 'mocha/raised_exception' +require 'mocha/return_values' +require 'mocha/thrown_object' +require 'mocha/yield_parameters' + +module Mocha + class Invocation + # @private + def initialize(method_name, yield_parameters = YieldParameters.new, return_values = ReturnValues.new) + @method_name = method_name + @yield_parameters = yield_parameters + @return_values = return_values + @yields = [] + @result = nil + end + + # @private + def call(*arguments) + @arguments = ParametersMatcher.new(arguments) + @yield_parameters.next_invocation.each do |yield_parameters| + @yields << ParametersMatcher.new(yield_parameters) + yield(*yield_parameters) + end + @return_values.next(self) + end + + # @private + def returned(value) + @result = value + end + + # @private + def raised(exception) + @result = RaisedException.new(exception) + end + + # @private + def threw(tag, value) + @result = ThrownObject.new(tag, value) + end + + # @private + def mocha_inspect + desc = "\n - #{@method_name}#{@arguments.mocha_inspect} # => #{@result.mocha_inspect}" + desc << " after yielding #{@yields.map(&:mocha_inspect).join(', then ')}" if @yields.any? + desc + end + end +end diff --git a/lib/mocha/mock.rb b/lib/mocha/mock.rb index 11490b07c..bf8d0ed95 100644 --- a/lib/mocha/mock.rb +++ b/lib/mocha/mock.rb @@ -305,11 +305,11 @@ def method_missing(symbol, *arguments, &block) raise NoMethodError, "undefined method `#{symbol}' for #{mocha_inspect} which responds like #{@responder.mocha_inspect}" end if (matching_expectation_allowing_invocation = all_expectations.match_allowing_invocation(symbol, *arguments)) - matching_expectation_allowing_invocation.invoke(&block) + matching_expectation_allowing_invocation.invoke(*arguments, &block) elsif (matching_expectation = all_expectations.match(symbol, *arguments)) || (!matching_expectation && !@everything_stubbed) if @unexpected_invocation.nil? @unexpected_invocation = UnexpectedInvocation.new(self, symbol, *arguments) - matching_expectation.invoke(&block) if matching_expectation + matching_expectation.invoke(*arguments, &block) if matching_expectation message = @unexpected_invocation.full_description message << @mockery.mocha_inspect else diff --git a/lib/mocha/raised_exception.rb b/lib/mocha/raised_exception.rb new file mode 100644 index 000000000..8a1ad9720 --- /dev/null +++ b/lib/mocha/raised_exception.rb @@ -0,0 +1,11 @@ +module Mocha + class RaisedException + def initialize(exception) + @exception = exception + end + + def mocha_inspect + "raised #{@exception}" + end + end +end diff --git a/lib/mocha/return_values.rb b/lib/mocha/return_values.rb index c7a0263a8..8479e87a5 100644 --- a/lib/mocha/return_values.rb +++ b/lib/mocha/return_values.rb @@ -12,11 +12,11 @@ def initialize(*values) @values = values end - def next + def next(invocation) case @values.length when 0 then nil - when 1 then @values.first.evaluate - else @values.shift.evaluate + when 1 then @values.first.evaluate(invocation) + else @values.shift.evaluate(invocation) end end diff --git a/lib/mocha/single_return_value.rb b/lib/mocha/single_return_value.rb index 42535274e..b7145c79a 100644 --- a/lib/mocha/single_return_value.rb +++ b/lib/mocha/single_return_value.rb @@ -6,7 +6,8 @@ def initialize(value) @value = value end - def evaluate + def evaluate(invocation) + invocation.returned(@value) @value end end diff --git a/lib/mocha/thrower.rb b/lib/mocha/thrower.rb index c51e0ed00..f3e4bc710 100644 --- a/lib/mocha/thrower.rb +++ b/lib/mocha/thrower.rb @@ -5,7 +5,8 @@ def initialize(tag, object = nil) @object = object end - def evaluate + def evaluate(invocation) + invocation.threw(@tag, @object) throw @tag, @object end end diff --git a/lib/mocha/thrown_object.rb b/lib/mocha/thrown_object.rb new file mode 100644 index 000000000..254b3dedf --- /dev/null +++ b/lib/mocha/thrown_object.rb @@ -0,0 +1,12 @@ +module Mocha + class ThrownObject + def initialize(tag, value = nil) + @tag = tag + @value = value + end + + def mocha_inspect + "threw (#{@tag.mocha_inspect}, #{@value.mocha_inspect})" + end + end +end diff --git a/test/acceptance/display_matching_invocations_alongside_expectations_test.rb b/test/acceptance/display_matching_invocations_alongside_expectations_test.rb new file mode 100644 index 000000000..9c3e4de8c --- /dev/null +++ b/test/acceptance/display_matching_invocations_alongside_expectations_test.rb @@ -0,0 +1,76 @@ +require File.expand_path('../acceptance_test_helper', __FILE__) +require 'mocha/setup' + +class DisplayMatchingInvocationsAlongsideExpectationsTest < Mocha::TestCase + include AcceptanceTest + + def setup + setup_acceptance_test + @original_env = ENV.to_hash + ENV['MOCHA_OPTIONS'] = 'verbose' + end + + def teardown + ENV.replace(@original_env) + teardown_acceptance_test + end + + def test_should_display_results + test_result = run_as_test do + foo = mock('foo') + foo.expects(:bar).with(1).returns('a') + foo.stubs(:bar).with(any_parameters).returns('f').raises(StandardError).throws(:tag, 'value') + + foo.bar(1, 2) + assert_raise(StandardError) { foo.bar(3, 4) } + assert_throws(:tag) { foo.bar(5, 6) } + end + assert_invocations( + test_result, + '- allowed any number of times, invoked 3 times: #.bar(any_parameters)', + ' - #.bar(1, 2) # => "f"', + ' - #.bar(3, 4) # => raised StandardError', + ' - #.bar(5, 6) # => threw (:tag, "value")' + ) + end + + def test_should_display_yields + test_result = run_as_test do + foo = mock('foo') + foo.expects(:bar).with(1).returns('a') + foo.stubs(:bar).with(any_parameters).multiple_yields(%w[b c], %w[d e]).returns('f').raises(StandardError).throws(:tag, 'value') + + foo.bar(1, 2) { |_ignored| } + assert_raise(StandardError) { foo.bar(3, 4) { |_ignored| } } + assert_throws(:tag) { foo.bar(5, 6) { |_ignored| } } + end + assert_invocations( + test_result, + '- allowed any number of times, invoked 3 times: #.bar(any_parameters)', + ' - #.bar(1, 2) # => "f" after yielding ("b", "c"), then ("d", "e")', + ' - #.bar(3, 4) # => raised StandardError after yielding ("b", "c"), then ("d", "e")', + ' - #.bar(5, 6) # => threw (:tag, "value") after yielding ("b", "c"), then ("d", "e")' + ) + end + + def test_should_display_empty_yield_and_return + test_result = run_as_test do + foo = mock('foo') + foo.expects(:bar).with(1).returns('a') + foo.stubs(:bar).with(any_parameters).yields + + foo.bar(1, 2) { |_ignored| } + end + assert_invocations( + test_result, + '- allowed any number of times, invoked once: #.bar(any_parameters)', + ' - #.bar(1, 2) # => nil after yielding ()' + ) + end + + def assert_invocations(test_result, *invocations) + assert_failed(test_result) + assert_equal invocations.unshift('satisfied expectations:'), + test_result.failure_message_lines[-invocations.size..-1] + end +end diff --git a/test/acceptance/exception_rescue_test.rb b/test/acceptance/exception_rescue_test.rb index e8f4a5608..54905503a 100644 --- a/test/acceptance/exception_rescue_test.rb +++ b/test/acceptance/exception_rescue_test.rb @@ -48,7 +48,7 @@ def test_unsatisfied_expectation_exception_is_not_caught_by_standard_rescue assert_equal [ 'not all expectations were satisfied', 'unsatisfied expectations:', - '- expected exactly once, not yet invoked: #.some_method(any_parameters)' + '- expected exactly once, invoked never: #.some_method(any_parameters)' ], test_result.failure_message_lines end end diff --git a/test/acceptance/expected_invocation_count_test.rb b/test/acceptance/expected_invocation_count_test.rb index fc177709a..9067df6a6 100644 --- a/test/acceptance/expected_invocation_count_test.rb +++ b/test/acceptance/expected_invocation_count_test.rb @@ -155,7 +155,7 @@ def test_should_fail_if_method_is_expected_at_least_once_but_is_never_called assert_equal [ 'not all expectations were satisfied', 'unsatisfied expectations:', - '- expected at least once, not yet invoked: #.method(any_parameters)' + '- expected at least once, invoked never: #.method(any_parameters)' ], test_result.failure_message_lines end @@ -224,7 +224,7 @@ def test_should_fail_fast_if_there_is_no_matching_expectation assert_equal [ 'unexpected invocation: #.method()', 'unsatisfied expectations:', - '- expected exactly once, not yet invoked: #.method(1)' + '- expected exactly once, invoked never: #.method(1)' ], test_result.failure_message_lines end end diff --git a/test/acceptance/stub_any_instance_method_defined_on_superclass_test.rb b/test/acceptance/stub_any_instance_method_defined_on_superclass_test.rb index 27152c6d0..8087e1c11 100644 --- a/test/acceptance/stub_any_instance_method_defined_on_superclass_test.rb +++ b/test/acceptance/stub_any_instance_method_defined_on_superclass_test.rb @@ -59,7 +59,7 @@ def my_instance_method; end assert_equal [ 'not all expectations were satisfied', 'unsatisfied expectations:', - '- expected exactly once, not yet invoked: #.my_instance_method(any_parameters)' + '- expected exactly once, invoked never: #.my_instance_method(any_parameters)' ], test_result.failure_message_lines end end diff --git a/test/acceptance/stub_class_method_defined_on_superclass_test.rb b/test/acceptance/stub_class_method_defined_on_superclass_test.rb index 14797c65c..6b4a74f5f 100644 --- a/test/acceptance/stub_class_method_defined_on_superclass_test.rb +++ b/test/acceptance/stub_class_method_defined_on_superclass_test.rb @@ -138,7 +138,7 @@ def self.my_class_method; end assert_equal [ 'not all expectations were satisfied', 'unsatisfied expectations:', - '- expected exactly once, not yet invoked: superklass.my_class_method(any_parameters)' + '- expected exactly once, invoked never: superklass.my_class_method(any_parameters)' ], test_result.failure_message_lines end # rubocop:enable Lint/DuplicateMethods diff --git a/test/integration/shared_tests.rb b/test/integration/shared_tests.rb index 00ea88292..f5ef1886f 100644 --- a/test/integration/shared_tests.rb +++ b/test/integration/shared_tests.rb @@ -63,7 +63,7 @@ def test_mock_object_unsatisfied_expectation assert_equal [ 'not all expectations were satisfied', 'unsatisfied expectations:', - '- expected exactly once, not yet invoked: #.expected(any_parameters)' + '- expected exactly once, invoked never: #.expected(any_parameters)' ], test_result.failure_message_lines end @@ -101,7 +101,7 @@ def test_mock_object_unsatisfied_expectation_in_setup assert_equal [ 'not all expectations were satisfied', 'unsatisfied expectations:', - '- expected exactly once, not yet invoked: #.expected(any_parameters)' + '- expected exactly once, invoked never: #.expected(any_parameters)' ], test_result.failure_message_lines end @@ -151,7 +151,7 @@ def test_real_object_unsatisfied_expectation assert_equal [ 'not all expectations were satisfied', 'unsatisfied expectations:', - "- expected exactly once, not yet invoked: #{object.mocha_inspect}.expected(any_parameters)" + "- expected exactly once, invoked never: #{object.mocha_inspect}.expected(any_parameters)" ], test_result.failure_message_lines end diff --git a/test/unit/cardinality_test.rb b/test/unit/cardinality_test.rb index ee62c8014..048115dec 100644 --- a/test/unit/cardinality_test.rb +++ b/test/unit/cardinality_test.rb @@ -1,43 +1,56 @@ require File.expand_path('../../test_helper', __FILE__) require 'mocha/cardinality' +require 'mocha/invocation' +require 'mocha/return_values' +require 'mocha/yield_parameters' class CardinalityTest < Mocha::TestCase include Mocha + def new_invocation + Invocation.new(:irrelevant) + end + def test_should_allow_invocations_if_invocation_count_has_not_yet_reached_maximum cardinality = Cardinality.new(2, 3) - assert cardinality.invocations_allowed?(0) - assert cardinality.invocations_allowed?(1) - assert cardinality.invocations_allowed?(2) - assert !cardinality.invocations_allowed?(3) + assert cardinality.invocations_allowed? + cardinality << new_invocation + assert cardinality.invocations_allowed? + cardinality << new_invocation + assert cardinality.invocations_allowed? + cardinality << new_invocation + assert !cardinality.invocations_allowed? end def test_should_be_satisfied_if_invocations_so_far_have_reached_required_threshold cardinality = Cardinality.new(2, 3) - assert !cardinality.satisfied?(0) - assert !cardinality.satisfied?(1) - assert cardinality.satisfied?(2) - assert cardinality.satisfied?(3) + assert !cardinality.satisfied? + cardinality << new_invocation + assert !cardinality.satisfied? + cardinality << new_invocation + assert cardinality.satisfied? + cardinality << new_invocation + assert cardinality.satisfied? end def test_should_describe_cardinality - assert_equal 'allowed any number of times', Cardinality.at_least(0).mocha_inspect + assert_equal 'allowed any number of times', Cardinality.at_least(0).anticipated_times - assert_equal 'expected at most once', Cardinality.at_most(1).mocha_inspect - assert_equal 'expected at most twice', Cardinality.at_most(2).mocha_inspect - assert_equal 'expected at most 3 times', Cardinality.at_most(3).mocha_inspect + assert_equal 'expected at most once', Cardinality.at_most(1).anticipated_times + assert_equal 'expected at most twice', Cardinality.at_most(2).anticipated_times + assert_equal 'expected at most 3 times', Cardinality.at_most(3).anticipated_times - assert_equal 'expected at least once', Cardinality.at_least(1).mocha_inspect - assert_equal 'expected at least twice', Cardinality.at_least(2).mocha_inspect - assert_equal 'expected at least 3 times', Cardinality.at_least(3).mocha_inspect + assert_equal 'expected at least once', Cardinality.at_least(1).anticipated_times + assert_equal 'expected at least twice', Cardinality.at_least(2).anticipated_times + assert_equal 'expected at least 3 times', Cardinality.at_least(3).anticipated_times - assert_equal 'expected never', Cardinality.exactly(0).mocha_inspect - assert_equal 'expected exactly once', Cardinality.exactly(1).mocha_inspect - assert_equal 'expected exactly twice', Cardinality.exactly(2).mocha_inspect - assert_equal 'expected exactly 3 times', Cardinality.times(3).mocha_inspect + assert_equal 'expected never', Cardinality.exactly(0).anticipated_times + assert_equal 'expected exactly once', Cardinality.exactly(1).anticipated_times + assert_equal 'expected exactly twice', Cardinality.exactly(2).anticipated_times + assert_equal 'expected exactly 3 times', Cardinality.times(3).anticipated_times - assert_equal 'expected between 2 and 4 times', Cardinality.times(2..4).mocha_inspect - assert_equal 'expected between 1 and 3 times', Cardinality.times(1..3).mocha_inspect + assert_equal 'expected between 2 and 4 times', Cardinality.times(2..4).anticipated_times + assert_equal 'expected between 1 and 3 times', Cardinality.times(1..3).anticipated_times end def test_should_need_verifying diff --git a/test/unit/exception_raiser_test.rb b/test/unit/exception_raiser_test.rb index bc481905f..a3f42314b 100644 --- a/test/unit/exception_raiser_test.rb +++ b/test/unit/exception_raiser_test.rb @@ -1,40 +1,45 @@ require File.expand_path('../../test_helper', __FILE__) +require 'mocha/invocation' require 'mocha/exception_raiser' require 'timeout' class ExceptionRaiserTest < Mocha::TestCase include Mocha + def new_invocation + Invocation.new(:irrelevant) + end + def test_should_raise_exception_with_specified_class_and_default_message exception_class = Class.new(StandardError) raiser = ExceptionRaiser.new(exception_class, nil) - exception = assert_raises(exception_class) { raiser.evaluate } + exception = assert_raises(exception_class) { raiser.evaluate(new_invocation) } assert_equal exception_class.to_s, exception.message end def test_should_raise_exception_with_specified_class_and_message exception_class = Class.new(StandardError) raiser = ExceptionRaiser.new(exception_class, 'message') - exception = assert_raises(exception_class) { raiser.evaluate } + exception = assert_raises(exception_class) { raiser.evaluate(new_invocation) } assert_equal 'message', exception.message end def test_should_raise_exception_instance exception_class = Class.new(StandardError) raiser = ExceptionRaiser.new(exception_class.new('message'), nil) - exception = assert_raises(exception_class) { raiser.evaluate } + exception = assert_raises(exception_class) { raiser.evaluate(new_invocation) } assert_equal 'message', exception.message end def test_should_raise_interrupt_exception_with_default_message_so_it_works_in_ruby_1_8_6 raiser = ExceptionRaiser.new(Interrupt, nil) - assert_raises(Interrupt) { raiser.evaluate } + assert_raises(Interrupt) { raiser.evaluate(new_invocation) } end def test_should_raise_subclass_of_interrupt_exception_with_default_message_so_it_works_in_ruby_1_8_6 exception_class = Class.new(Interrupt) raiser = ExceptionRaiser.new(exception_class, nil) - assert_raises(exception_class) { raiser.evaluate } + assert_raises(exception_class) { raiser.evaluate(new_invocation) } end end diff --git a/test/unit/expectation_test.rb b/test/unit/expectation_test.rb index 3a37dcd7e..8363df14a 100644 --- a/test/unit/expectation_test.rb +++ b/test/unit/expectation_test.rb @@ -283,7 +283,7 @@ def test_should_verify_successfully_if_expected_call_was_made_at_least_once def test_should_not_verify_successfully_if_expected_call_was_not_made_at_least_once expectation = new_expectation.with(1, 2, 3).at_least_once assert !expectation.verified? - assert_match(/expected at least once, not yet invoked/i, expectation.mocha_inspect) + assert_match(/expected at least once, invoked never/i, expectation.mocha_inspect) end def test_should_verify_successfully_if_expected_call_was_made_expected_number_of_times diff --git a/test/unit/return_values_test.rb b/test/unit/return_values_test.rb index 375427b32..c43aaca61 100644 --- a/test/unit/return_values_test.rb +++ b/test/unit/return_values_test.rb @@ -1,61 +1,66 @@ require File.expand_path('../../test_helper', __FILE__) +require 'mocha/invocation' require 'mocha/return_values' class ReturnValuesTest < Mocha::TestCase include Mocha + def new_invocation + Invocation.new(:irrelevant) + end + def test_should_return_nil values = ReturnValues.new - assert_nil values.next + assert_nil values.next(new_invocation) end def test_should_keep_returning_nil values = ReturnValues.new - values.next - assert_nil values.next - assert_nil values.next + values.next(new_invocation) + assert_nil values.next(new_invocation) + assert_nil values.next(new_invocation) end def test_should_return_evaluated_single_return_value values = ReturnValues.new(SingleReturnValue.new('value')) - assert_equal 'value', values.next + assert_equal 'value', values.next(new_invocation) end def test_should_keep_returning_evaluated_single_return_value values = ReturnValues.new(SingleReturnValue.new('value')) - values.next - assert_equal 'value', values.next - assert_equal 'value', values.next + values.next(new_invocation) + assert_equal 'value', values.next(new_invocation) + assert_equal 'value', values.next(new_invocation) end def test_should_return_consecutive_evaluated_single_return_values values = ReturnValues.new(SingleReturnValue.new('value_1'), SingleReturnValue.new('value_2')) - assert_equal 'value_1', values.next - assert_equal 'value_2', values.next + assert_equal 'value_1', values.next(new_invocation) + assert_equal 'value_2', values.next(new_invocation) end def test_should_keep_returning_last_of_consecutive_evaluated_single_return_values values = ReturnValues.new(SingleReturnValue.new('value_1'), SingleReturnValue.new('value_2')) - values.next - values.next - assert_equal 'value_2', values.next - assert_equal 'value_2', values.next + values.next(new_invocation) + values.next(new_invocation) + assert_equal 'value_2', values.next(new_invocation) + assert_equal 'value_2', values.next(new_invocation) end def test_should_build_single_return_values_for_each_values values = ReturnValues.build('value_1', 'value_2', 'value_3').values - assert_equal 'value_1', values[0].evaluate - assert_equal 'value_2', values[1].evaluate - assert_equal 'value_3', values[2].evaluate + assert_equal 'value_1', values[0].evaluate(new_invocation) + assert_equal 'value_2', values[1].evaluate(new_invocation) + assert_equal 'value_3', values[2].evaluate(new_invocation) end def test_should_combine_two_sets_of_return_values values1 = ReturnValues.build('value_1') values2 = ReturnValues.build('value_2a', 'value_2b') values = (values1 + values2).values - assert_equal 'value_1', values[0].evaluate - assert_equal 'value_2a', values[1].evaluate - assert_equal 'value_2b', values[2].evaluate + assert_equal 'value_1', values[0].evaluate(new_invocation) + assert_equal 'value_2a', values[1].evaluate(new_invocation) + assert_equal 'value_2b', values[2].evaluate(new_invocation) end end diff --git a/test/unit/single_return_value_test.rb b/test/unit/single_return_value_test.rb index 6941f91bb..6e7153c19 100644 --- a/test/unit/single_return_value_test.rb +++ b/test/unit/single_return_value_test.rb @@ -1,12 +1,17 @@ require File.expand_path('../../test_helper', __FILE__) +require 'mocha/invocation' require 'mocha/single_return_value' class SingleReturnValueTest < Mocha::TestCase include Mocha + def new_invocation + Invocation.new(:irrelevant) + end + def test_should_return_value value = SingleReturnValue.new('value') - assert_equal 'value', value.evaluate + assert_equal 'value', value.evaluate(new_invocation) end end diff --git a/test/unit/thrower_test.rb b/test/unit/thrower_test.rb index 2a9572302..cf1b4786c 100644 --- a/test/unit/thrower_test.rb +++ b/test/unit/thrower_test.rb @@ -1,18 +1,23 @@ require File.expand_path('../../test_helper', __FILE__) +require 'mocha/invocation' require 'mocha/thrower' class ThrowerTest < Mocha::TestCase include Mocha + def new_invocation + Invocation.new(:irrelevant) + end + def test_should_throw_tag thrower = Thrower.new(:tag) - assert_throws(:tag) { thrower.evaluate } + assert_throws(:tag) { thrower.evaluate(new_invocation) } end def test_should_throw_tag_with_return_value thrower = Thrower.new(:tag, 'return-value') - return_value = catch(:tag) { thrower.evaluate } + return_value = catch(:tag) { thrower.evaluate(new_invocation) } assert_equal 'return-value', return_value end end