Browse files

Raise an exception when stubbed method is called with wrong arguments

- Closes #6.
  • Loading branch information...
1 parent c5a6007 commit c92d52be983eeb33d6b739ae5e37e95e5de8deb8 Alexey committed with dchelimsky Nov 1, 2010
Showing with 13 additions and 6 deletions.
  1. +1 −1 lib/spec/mocks/message_expectation.rb
  2. +10 −3 lib/spec/mocks/proxy.rb
  3. +2 −2 spec/spec/mocks/stub_spec.rb
View
2 lib/spec/mocks/message_expectation.rb
@@ -239,7 +239,7 @@ def similar_messages
@similar_messages ||= []
end
- def advise(args, block)
+ def advise(*args)
similar_messages << args
end
View
13 lib/spec/mocks/proxy.rb
@@ -105,14 +105,17 @@ def message_received(sym, *args, &block)
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 @@ def raise_unexpected_message_error(sym, *args)
def find_matching_method_stub(sym, *args)
@stubs.find {|stub| stub.matches(sym, args)}
end
-
+
private
def __add(sym)
@@ -242,6 +245,10 @@ def find_almost_matching_expectation(sym, *args)
@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
View
4 spec/spec/mocks/stub_spec.rb
@@ -165,13 +165,13 @@ def existing_instance_method
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

0 comments on commit c92d52b

Please sign in to comment.