Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Issue #123: Improved error message when user forgets to stub a method…

… with default behavior.
  • Loading branch information...
commit 409955f5b9e49d116feca8e41f2721f955a3c940 1 parent 4b70b45
Neha Kumari authored
View
7 lib/rspec/mocks/error_generator.rb
@@ -28,6 +28,13 @@ def raise_unexpected_message_args_error(expectation, *args)
end
# @private
+ def raise_missing_default_stub_error(expectation,*args)
+ expected_args = format_args(*expectation.expected_args)
+ actual_args = args.collect {|a| format_args(*a)}.join(", ")
+ __raise "#{intro} received #{expectation.message.inspect} with unexpected arguments\n Please stub a default value first if message might be received with other args as well. \n"
+ end
+
+ # @private
def raise_similar_message_args_error(expectation, *args)
expected_args = format_args(*expectation.expected_args)
actual_args = args.collect {|a| format_args(*a)}.join(", ")
View
11 lib/rspec/mocks/proxy.rb
@@ -129,6 +129,8 @@ def message_received(message, *args, &block)
elsif stub = find_almost_matching_stub(message, *args)
stub.advise(*args)
raise_unexpected_message_args_error(stub, *args)
+ elsif expectation = find_almost_matching_satisfied_expectation(message,*args)
+ raise_missing_default_stub_error(expectation, *args) if not null_object?
elsif @object.is_a?(Class)
@object.superclass.__send__(message, *args, &block)
else
@@ -146,6 +148,11 @@ def raise_unexpected_message_error(method_name, *args)
@error_generator.raise_unexpected_message_error method_name, *args
end
+ # @private
+ def raise_missing_default_stub_error(expectation, *args)
+ @error_generator.raise_missing_default_stub_error(expectation, *args)
+ end
+
private
def method_double
@@ -165,6 +172,10 @@ def find_almost_matching_expectation(method_name, *args)
method_double[method_name].expectations.find {|expectation| expectation.matches_name_but_not_args(method_name, *args) && !expectation.called_max_times?}
end
+ def find_almost_matching_satisfied_expectation(method_name, *args)
+ method_double[method_name].expectations.find {|expectation| expectation.matches_name_but_not_args(method_name, *args) }
+ end
+
def find_matching_method_stub(method_name, *args)
method_double[method_name].stubs.find {|stub| stub.matches?(method_name, *args)}
end
View
10 spec/rspec/mocks/bug_report_123_spec.rb
@@ -0,0 +1,10 @@
+require 'spec_helper'
+
+describe 'with' do
+ it "should ask the user to stub a default value first if the message might be received with other args as well" do
+ obj = Object.new
+ obj.should_receive(:foobar).with(1)
+ obj.foobar(1)
+ lambda{ obj.foobar(2) }.should raise_error(RSpec::Mocks::MockExpectationError, /Please stub a default value first if message might be received with other args as well./)
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.