Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

any_instance is supported when a class overrides Object#method

* Fixes #180
* Closes #181
  • Loading branch information...
commit 645b15867ef015daa72ff77a0110b455017f5ee4 1 parent 3e3e64d
@alindeman alindeman authored
View
2  Changelog.md
@@ -5,6 +5,8 @@ Bug fixes
* Fix `:transfer_nested_constants` option of `stub_const` so that it
doesn't blow up when there are inherited constants. (Myron Marston)
+* `any_instance` stubs can be used on classes that override `Object#method`.
+ (Andy Lindeman)
Deprecations:
View
4 lib/rspec/mocks/any_instance/recorder.rb
@@ -175,7 +175,7 @@ def observe!(method_name)
backup_method!(method_name)
@klass.class_eval(<<-EOM, __FILE__, __LINE__)
def #{method_name}(*args, &blk)
- klass = self.method(:#{method_name}).owner
+ klass = ::Object.instance_method(:method).bind(self).call(:#{method_name}).owner
klass.__recorder.playback!(self, :#{method_name})
self.__send__(:#{method_name}, *args, &blk)
end
@@ -187,7 +187,7 @@ def mark_invoked!(method_name)
@klass.class_eval(<<-EOM, __FILE__, __LINE__)
def #{method_name}(*args, &blk)
method_name = :#{method_name}
- klass = self.method(:#{method_name}).owner
+ klass = ::Object.instance_method(:method).bind(self).call(:#{method_name}).owner
invoked_instance = klass.__recorder.instance_that_received(method_name)
raise RSpec::Mocks::MockExpectationError, "The message '#{method_name}' was received by \#{self.inspect} but has already been received by \#{invoked_instance}"
end
View
14 spec/rspec/mocks/any_instance_spec.rb
@@ -845,6 +845,20 @@ class RSpec::SampleRspecTestClass;end
end.to raise_error(RSpec::Mocks::MockExpectationError, "The message 'existing_method' was received by #{instance_two.inspect} but has already been received by #{instance_one.inspect}")
end
end
+
+ context "when a class overrides Object#method" do
+ let(:http_request_class) { Struct.new(:method, :uri) }
+
+ it "stubs the method correctly" do
+ http_request_class.any_instance.stub(:existing_method).and_return("foo")
+ http_request_class.new.existing_method.should == "foo"
+ end
+
+ it "mocks the method correctly" do
+ http_request_class.any_instance.should_receive(:existing_method).and_return("foo")
+ http_request_class.new.existing_method.should == "foo"
+ end
+ end
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.