Permalink
Browse files

Improve failure message for unexpected arguments

  • Loading branch information...
1 parent 3cb4ccc commit 138d298cd63fec6ac48722b5b778152c76c0b040 @jferris jferris committed Mar 27, 2013
@@ -59,5 +59,5 @@ Feature: Spy on a stubbed method on a pure mock
end
"""
When I run `rspec failed_message_expectations_spec.rb`
- Then the output should contain "expected: 1 time"
- And the output should contain "received: 0 times"
+ Then the output should contain "expected: (:expected, :arguments)"
+ And the output should contain "got: (:unexpected)"
@@ -11,13 +11,15 @@ def initialize(method_name)
end
def matches?(subject)
- @expectation = expect(subject)
+ @subject = subject
+ @expectation = expect
@expectation.expected_messages_received?
end
def does_not_match?(subject)
+ @subject = subject
ensure_count_unconstrained
- @expectation = expect(subject).never
+ @expectation = expect.never
@expectation.expected_messages_received?
end
@@ -38,8 +40,8 @@ def negative_failure_message
private
- def expect(subject)
- build_expectation(subject) do |expectation|
+ def expect
+ build_expectation do |expectation|
apply_constraints_to expectation
end
end
@@ -64,14 +66,18 @@ def count_constrait
end
def generate_failure_message
+ mock_proxy.check_for_unexpected_arguments(@expectation)
@expectation.generate_error
rescue RSpec::Mocks::MockExpectationError => error
error.message
end
- def build_expectation(subject, &block)
- subject.__send__(:__mock_proxy).
- build_expectation(@method_name, &block)
+ def build_expectation(&block)
+ mock_proxy.build_expectation(@method_name, &block)
+ end
+
+ def mock_proxy
+ @subject.__send__(:__mock_proxy)
end
end
end
@@ -105,6 +105,15 @@ def build_expectation(method_name)
end
# @private
+ def check_for_unexpected_arguments(expectation)
+ @messages_received.each do |(method_name, args, block)|
+ if expectation.matches_name_but_not_args(method_name, *args)
+ raise_unexpected_message_args_error(expectation, *args)
+ end
+ end
+ end
+
+ # @private
def add_stub(location, method_name, opts={}, &implementation)
method_double[method_name].add_stub @error_generator, @expectation_ordering, location, opts, &implementation
end
@@ -71,7 +71,7 @@ module Mocks
expect {
expect(dbl).to have_received(:expected_method).with(:unexpected, :args)
- }.to raise_error(/expected: 1 time/) # TODO: better failure message
+ }.to raise_error(/with unexpected arguments/)
end
end

0 comments on commit 138d298

Please sign in to comment.