Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix edge case bug exposed w/ `should_receive` on a null object w/ a p…

…revious stub.

In a case like:

  double = stub.as_null_object
  double.stub(:foo => "bar")
  double.should_receive(:foo)
  double.foo

..it used to return "bar", but due to my changes in a5f296e it was returning `self`.
  • Loading branch information...
commit 26f64769c7accbc73a41c55a8d3afcfb9f208fdb 1 parent b35b5b4
@myronmarston myronmarston authored
View
9 Changelog.md
@@ -1,3 +1,12 @@
+### dev
+
+Bug fixes
+
+* Fix `should_receive` so that when it is called on an `as_null_object`
+ double with no implementation, and there is a previous explicit stub
+ for the same method, the explicit stub remains (rather than being
+ overriden with the null object implementation--`return self`). (Myron Marston)
+
### 2.11.0 / 2012-07-07
[full changelog](http://github.com/rspec/rspec-mocks/compare/v2.10.1...v2.11.0)
View
6 lib/rspec/mocks/method_double.rb
@@ -159,6 +159,12 @@ def add_stub(error_generator, expectation_ordering, expected_from, opts={}, &imp
end
# @private
+ def add_default_stub(*args, &implementation)
+ return if stubs.any?
+ add_stub(*args, &implementation)
+ end
+
+ # @private
def remove_stub
raise_method_not_stubbed_error if stubs.empty?
expectations.empty? ? reset : stubs.clear
View
11 lib/rspec/mocks/proxy.rb
@@ -65,8 +65,15 @@ def already_proxied_respond_to?
# @private
def add_message_expectation(location, method_name, opts={}, &block)
- block ||= Proc.new { @object } if null_object?
- method_double[method_name].add_expectation @error_generator, @expectation_ordering, location, opts, &block
+ meth_double = method_double[method_name]
+
+ if null_object? && !block
+ meth_double.add_default_stub(@error_generator, @expectation_ordering, location, opts) do
+ @object
+ end
+ end
+
+ meth_double.add_expectation @error_generator, @expectation_ordering, location, opts, &block
end
# @private
View
6 spec/rspec/mocks/null_object_mock_spec.rb
@@ -50,6 +50,12 @@ module Mocks
@double.foo.bar.should be(@double)
end
+ it 'returns an explicitly stubbed value from an expectation with no implementation' do
+ @double.stub(:foo => "bar")
+ @double.should_receive(:foo)
+ @double.foo.should eq("bar")
+ end
+
it "fails verification when explicit exception not met" do
lambda do
@double.should_receive(:something)
Please sign in to comment.
Something went wrong with that request. Please try again.