> In RSpec-2 it raises an informative error, so I think this is what we > should do in RSpec-1. Would you like to submit a patch to make it do > that? > > Sure. Attaching a patch. Also submitted a pull request on github - whatever.
From 030d8e7590ed69c4ddee70b9ea5660933a8e9c4d Mon Sep 17 00:00:00 2001 From: Alexey <ale...@alexey-desktop.(none)> Date: Mon, 1 Nov 2010 21:39:16 +0300 Subject: [PATCH] Raising an exception when stubbed method is called with wrong arguments
--- lib/spec/mocks/message_expectation.rb | 2 +- lib/spec/mocks/proxy.rb | 13 ++++++++++--- spec/spec/mocks/stub_spec.rb | 4 ++-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/spec/mocks/message_expectation.rb b/lib/spec/mocks/message_expectation.rb index cd8dc0f..3d0189a 100644 --- a/lib/spec/mocks/message_expectation.rb +++ b/lib/spec/mocks/message_expectation.rb @@ -239,7 +239,7 @@ module Spec @similar_messages ||= [] end - def advise(args, block) + def advise(*args) similar_messages << args end diff --git a/lib/spec/mocks/proxy.rb b/lib/spec/mocks/proxy.rb index d3ffbcb..f63dda0 100644 --- a/lib/spec/mocks/proxy.rb +++ b/lib/spec/mocks/proxy.rb @@ -105,14 +105,17 @@ module Spec if (stub && expectation && expectation.called_max_times?) || (stub && !expectation) if expectation = find_almost_matching_expectation(sym, *args) - expectation.advise(args, block) unless expectation.expected_messages_received? + expectation.advise(*args) unless expectation.expected_messages_received? end stub.invoke(*args, &block) elsif expectation expectation.invoke(*args, &block) elsif expectation = find_almost_matching_expectation(sym, *args) - expectation.advise(args, block) if null_object? unless expectation.expected_messages_received? + expectation.advise(*args) if null_object? unless expectation.expected_messages_received? raise_unexpected_message_args_error(expectation, *args) unless (has_negative_expectation?(sym) or null_object?) + elsif stub = find_almost_matching_stub(sym, *args) + stub.advise(*args) + raise_unexpected_message_args_error(stub, *args) elsif @target.is_a?(Class) @target.superclass.send(sym, *args, &block) else @@ -131,7 +134,7 @@ module Spec def find_matching_method_stub(sym, *args) @stubs.find {|stub| stub.matches(sym, args)} end - + private def __add(sym) @@ -242,6 +245,10 @@ module Spec @expectations.find {|expectation| expectation.matches_name_but_not_args(sym, args)} end + def find_almost_matching_stub(sym, *args) + @stubs.find {|stub| stub.matches_name_but_not_args(sym, args)} + end + end end end diff --git a/spec/spec/mocks/stub_spec.rb b/spec/spec/mocks/stub_spec.rb index e0b7b26..43a0800 100644 --- a/spec/spec/mocks/stub_spec.rb +++ b/spec/spec/mocks/stub_spec.rb @@ -165,13 +165,13 @@ module Spec it "should complain if called with no arg" do lambda do @stub.foo - end.should raise_error + end.should raise_error(/received :foo with unexpected arguments/) end it "should complain if called with other arg" do lambda do @stub.foo("other") - end.should raise_error + end.should raise_error(/received :foo with unexpected arguments/) end it "should not complain if also mocked w/ different args" do -- 1.6.3.3
_______________________________________________ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users