Permalink
Browse files

Include unexpected invocation and change the language slightly to mak…

  • Loading branch information...
floehopper committed Nov 30, 2010
1 parent 2b70431 commit afea438fe5a85eadf2ebfd61bfd17c28393604db
@@ -458,9 +458,9 @@ def mocha_inspect
message = "#{@cardinality.mocha_inspect}, "
message << case @invocation_count
when 0 then "not yet invoked"
when 1 then "already invoked once"
when 2 then "already invoked twice"
else "already invoked #{@invocation_count} times"
when 1 then "invoked once"
when 2 then "invoked twice"
else "invoked #{@invocation_count} times"
end
message << ": "
message << method_signature
@@ -158,6 +158,7 @@ def method_missing(symbol, *arguments, &block)
matching_expectation_allowing_invocation.invoke(&block)
else
if (matching_expectation = @expectations.match(symbol, *arguments)) || (!matching_expectation && !@everything_stubbed)
matching_expectation.invoke(&block) if matching_expectation
message = UnexpectedInvocation.new(self, symbol, *arguments).to_s
message << Mockery.instance.mocha_inspect
raise ExpectationError.new(message, caller)
@@ -29,7 +29,12 @@ def test_should_fail_fast_if_method_is_never_expected_but_is_called_once
1.times { mock.method }
end
assert_failed(test_result)
assert_equal ["unexpected invocation: #<Mock:mock>.method()\nsatisfied expectations:\n- expected never, not yet invoked: #<Mock:mock>.method(any_parameters)\n"], test_result.failure_messages
failure_message_lines = test_result.failure_messages.first.split("\n")
assert_equal [
"unexpected invocation: #<Mock:mock>.method()",
"unsatisfied expectations:",
"- expected never, invoked once: #<Mock:mock>.method(any_parameters)"
], failure_message_lines
end

def test_should_pass_if_method_is_expected_twice_and_is_called_twice
@@ -48,7 +53,7 @@ def test_should_fail_if_method_is_expected_twice_but_is_called_once
1.times { mock.method }
end
assert_failed(test_result)
assert_equal ["not all expectations were satisfied\nunsatisfied expectations:\n- expected exactly twice, already invoked once: #<Mock:mock>.method(any_parameters)\n"], test_result.failure_messages
assert_equal ["not all expectations were satisfied\nunsatisfied expectations:\n- expected exactly twice, invoked once: #<Mock:mock>.method(any_parameters)\n"], test_result.failure_messages
end

def test_should_fail_fast_if_method_is_expected_twice_but_is_called_three_times
@@ -58,7 +63,12 @@ def test_should_fail_fast_if_method_is_expected_twice_but_is_called_three_times
3.times { mock.method }
end
assert_failed(test_result)
assert_equal ["unexpected invocation: #<Mock:mock>.method()\nsatisfied expectations:\n- expected exactly twice, already invoked twice: #<Mock:mock>.method(any_parameters)\n"], test_result.failure_messages
failure_message_lines = test_result.failure_messages.first.split("\n")
assert_equal [
"unexpected invocation: #<Mock:mock>.method()",
"unsatisfied expectations:",
"- expected exactly twice, invoked 3 times: #<Mock:mock>.method(any_parameters)"
], failure_message_lines
end

def test_should_pass_if_method_is_expected_between_two_and_four_times_and_is_called_twice
@@ -95,7 +105,7 @@ def test_should_fail_if_method_is_expected_between_two_and_four_times_and_is_cal
1.times { mock.method }
end
assert_failed(test_result)
assert_equal ["not all expectations were satisfied\nunsatisfied expectations:\n- expected between 2 and 4 times, already invoked once: #<Mock:mock>.method(any_parameters)\n"], test_result.failure_messages
assert_equal ["not all expectations were satisfied\nunsatisfied expectations:\n- expected between 2 and 4 times, invoked once: #<Mock:mock>.method(any_parameters)\n"], test_result.failure_messages
end

def test_should_fail_fast_if_method_is_expected_between_two_and_four_times_and_is_called_five_times
@@ -105,7 +115,12 @@ def test_should_fail_fast_if_method_is_expected_between_two_and_four_times_and_i
5.times { mock.method }
end
assert_failed(test_result)
assert_equal ["unexpected invocation: #<Mock:mock>.method()\nsatisfied expectations:\n- expected between 2 and 4 times, already invoked 4 times: #<Mock:mock>.method(any_parameters)\n"], test_result.failure_messages
failure_message_lines = test_result.failure_messages.first.split("\n")
assert_equal [
"unexpected invocation: #<Mock:mock>.method()",
"unsatisfied expectations:",
"- expected between 2 and 4 times, invoked 5 times: #<Mock:mock>.method(any_parameters)"
], failure_message_lines
end

def test_should_pass_if_method_is_expected_at_least_once_and_is_called_once
@@ -161,7 +176,12 @@ def test_should_fail_fast_if_method_is_expected_at_most_once_but_is_called_twice
2.times { mock.method }
end
assert_failed(test_result)
assert_equal ["unexpected invocation: #<Mock:mock>.method()\nsatisfied expectations:\n- expected at most once, already invoked once: #<Mock:mock>.method(any_parameters)\n"], test_result.failure_messages
failure_message_lines = test_result.failure_messages.first.split("\n")
assert_equal [
"unexpected invocation: #<Mock:mock>.method()",
"unsatisfied expectations:",
"- expected at most once, invoked twice: #<Mock:mock>.method(any_parameters)"
], failure_message_lines
end

def test_should_pass_if_method_is_never_expected_and_is_never_called_even_if_everything_is_stubbed
@@ -180,7 +200,12 @@ def test_should_fail_fast_if_method_is_never_expected_but_is_called_once_even_if
1.times { stub.method }
end
assert_failed(test_result)
assert_equal ["unexpected invocation: #<Mock:stub>.method()\nsatisfied expectations:\n- expected never, not yet invoked: #<Mock:stub>.method(any_parameters)\n"], test_result.failure_messages
failure_message_lines = test_result.failure_messages.first.split("\n")
assert_equal [
"unexpected invocation: #<Mock:stub>.method()",
"unsatisfied expectations:",
"- expected never, invoked once: #<Mock:stub>.method(any_parameters)"
], failure_message_lines
end

def test_should_fail_fast_if_there_is_no_matching_expectation
@@ -0,0 +1,71 @@
require File.expand_path('../acceptance_test_helper', __FILE__)
require 'mocha'

class FailureMessageTest < Test::Unit::TestCase

include AcceptanceTest

def setup
setup_acceptance_test
end

def teardown
teardown_acceptance_test
end

def test_should_include_unexpected_invocation_in_unsatisfied_expectation_message
test_result = run_as_test do
mock = mock('mock')
mock.expects(:method_one).once
2.times { mock.method_one }
end
assert_failed(test_result)
failure_message_lines = test_result.failure_messages.first.split("\n")
assert_equal [
"unexpected invocation: #<Mock:mock>.method_one()",
"unsatisfied expectations:",
"- expected exactly once, invoked twice: #<Mock:mock>.method_one(any_parameters)"
], failure_message_lines
end

def test_should_report_satisfied_expectations_as_well_as_unsatisfied_expectations
test_result = run_as_test do
mock = mock('mock')
mock.expects(:method_one).once
mock.expects(:method_two).twice
1.times { mock.method_one }
1.times { mock.method_two }
end
assert_failed(test_result)
failure_message_lines = test_result.failure_messages.first.split("\n")
assert_equal [
"not all expectations were satisfied",
"unsatisfied expectations:",
"- expected exactly twice, invoked once: #<Mock:mock>.method_two(any_parameters)",
"satisfied expectations:",
"- expected exactly once, invoked once: #<Mock:mock>.method_one(any_parameters)"
], failure_message_lines
end

def test_should_report_multiple_satisfied_expectations
test_result = run_as_test do
mock = mock('mock')
mock.expects(:method_one).once
mock.expects(:method_two).twice
mock.expects(:method_three).times(3)
1.times { mock.method_one }
2.times { mock.method_two }
2.times { mock.method_three }
end
assert_failed(test_result)
failure_message_lines = test_result.failure_messages.first.split("\n")
assert_equal [
"not all expectations were satisfied",
"unsatisfied expectations:",
"- expected exactly 3 times, invoked twice: #<Mock:mock>.method_three(any_parameters)",
"satisfied expectations:",
"- expected exactly twice, invoked twice: #<Mock:mock>.method_two(any_parameters)",
"- expected exactly once, invoked once: #<Mock:mock>.method_one(any_parameters)"
], failure_message_lines
end
end
@@ -296,7 +296,7 @@ def test_should_not_verify_successfully_if_expected_call_was_made_too_few_times
expectation = new_expectation.times(2)
1.times {expectation.invoke}
assert !expectation.verified?
assert_match(/expected exactly twice, already invoked once/i, expectation.mocha_inspect)
assert_match(/expected exactly twice, invoked once/i, expectation.mocha_inspect)
end

def test_should_not_verify_successfully_if_expected_call_was_made_too_many_times

0 comments on commit afea438

Please sign in to comment.