Skip to content
This repository
Browse code

Merge pull request #147 from c42engineering/issue123

Issue #123: Improved error message when user forgets to stub a method with a default.
  • Loading branch information...
commit 1ebc6984827ef343576c6b6080977ee53c5d07b5 2 parents 4b70b45 + 409955f
David Chelimsky dchelimsky authored
7 lib/rspec/mocks/error_generator.rb
@@ -28,6 +28,13 @@ def raise_unexpected_message_args_error(expectation, *args)
28 28 end
29 29
30 30 # @private
  31 + def raise_missing_default_stub_error(expectation,*args)
  32 + expected_args = format_args(*expectation.expected_args)
  33 + actual_args = args.collect {|a| format_args(*a)}.join(", ")
  34 + __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"
  35 + end
  36 +
  37 + # @private
31 38 def raise_similar_message_args_error(expectation, *args)
32 39 expected_args = format_args(*expectation.expected_args)
33 40 actual_args = args.collect {|a| format_args(*a)}.join(", ")
11 lib/rspec/mocks/proxy.rb
@@ -129,6 +129,8 @@ def message_received(message, *args, &block)
129 129 elsif stub = find_almost_matching_stub(message, *args)
130 130 stub.advise(*args)
131 131 raise_unexpected_message_args_error(stub, *args)
  132 + elsif expectation = find_almost_matching_satisfied_expectation(message,*args)
  133 + raise_missing_default_stub_error(expectation, *args) if not null_object?
132 134 elsif @object.is_a?(Class)
133 135 @object.superclass.__send__(message, *args, &block)
134 136 else
@@ -146,6 +148,11 @@ def raise_unexpected_message_error(method_name, *args)
146 148 @error_generator.raise_unexpected_message_error method_name, *args
147 149 end
148 150
  151 + # @private
  152 + def raise_missing_default_stub_error(expectation, *args)
  153 + @error_generator.raise_missing_default_stub_error(expectation, *args)
  154 + end
  155 +
149 156 private
150 157
151 158 def method_double
@@ -165,6 +172,10 @@ def find_almost_matching_expectation(method_name, *args)
165 172 method_double[method_name].expectations.find {|expectation| expectation.matches_name_but_not_args(method_name, *args) && !expectation.called_max_times?}
166 173 end
167 174
  175 + def find_almost_matching_satisfied_expectation(method_name, *args)
  176 + method_double[method_name].expectations.find {|expectation| expectation.matches_name_but_not_args(method_name, *args) }
  177 + end
  178 +
168 179 def find_matching_method_stub(method_name, *args)
169 180 method_double[method_name].stubs.find {|stub| stub.matches?(method_name, *args)}
170 181 end
10 spec/rspec/mocks/bug_report_123_spec.rb
... ... @@ -0,0 +1,10 @@
  1 +require 'spec_helper'
  2 +
  3 +describe 'with' do
  4 + it "should ask the user to stub a default value first if the message might be received with other args as well" do
  5 + obj = Object.new
  6 + obj.should_receive(:foobar).with(1)
  7 + obj.foobar(1)
  8 + 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./)
  9 + end
  10 +end

0 comments on commit 1ebc698

Please sign in to comment.
Something went wrong with that request. Please try again.