Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Raise with unexpected args message instead of NoMethodError when a stub
is specified with args but received with different args.
  • Loading branch information
dchelimsky committed Oct 3, 2010
1 parent d03d399 commit 409b2d0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
7 changes: 7 additions & 0 deletions lib/rspec/mocks/proxy.rb
Expand Up @@ -105,6 +105,9 @@ def message_received(method_name, *args, &block)
elsif expectation = find_almost_matching_expectation(method_name, *args)
expectation.advise(*args) if null_object? unless expectation.expected_messages_received?
raise_unexpected_message_args_error(expectation, *args) unless (has_negative_expectation?(method_name) or null_object?)
elsif stub = find_almost_matching_stub(method_name, *args)
stub.advise(*args)
raise_unexpected_message_args_error(stub, *args)
elsif @object.is_a?(Class)
@object.superclass.send(method_name, *args, &block)
else
Expand Down Expand Up @@ -145,6 +148,10 @@ def find_matching_method_stub(method_name, *args)
method_double[method_name].stubs.find {|stub| stub.matches?(method_name, *args)}
end

def find_almost_matching_stub(method_name, *args)
method_double[method_name].stubs.find {|stub| stub.matches_name_but_not_args(method_name, *args)}
end

end
end
end
4 changes: 2 additions & 2 deletions spec/rspec/mocks/stub_spec.rb
Expand Up @@ -199,13 +199,13 @@ def existing_private_instance_method
it "complains 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 "complains 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 "does not complain if also mocked w/ different args" do
Expand Down

0 comments on commit 409b2d0

Please sign in to comment.