Skip to content

Loading…

any_instance is supported when a class overrides Object#method #181

Closed
wants to merge 1 commit into from

2 participants

@alindeman
@myronmarston
RSpec member

Looks great. FWIW, I have a class in VCR that defines an argument-less method (i.e. VCR::Request#method which returns :get, :post, :put, :delete, etc), but at one point I realized that it would cause problems with meta-tools like rspec-mocks and I changed it to use a similar technique to what you've done here so that when called with an argument, it delegates to Object#method. It might be worth opening a PR with ActiveMerchant to have it do the same so that it can work better with other tools.

@myronmarston myronmarston commented on the diff
spec/rspec/mocks/any_instance_spec.rb
@@ -845,6 +845,26 @@ 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
+ before do
+ klass.class_eval <<-EOM
+ def method
+ "this is not awesome but does happen in real code"
+ end
+ EOM
+ end
@myronmarston RSpec member

One suggestion -- this doesn't really communicate much about a real situation where #method would be overriden. Instead, you might consider doing something like:

let(:http_request_class) { Struct.new(:method, :uri) }

This communicates better (I think an HTTP request object is the main time you'd see method overriden since it's a proper term in that domain), is less code, and is a bit less "meta" (e.g. no class_eval).

:thumbsup: I like it. I'll change, squash and merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@alindeman alindeman closed this in 645b158
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 22 additions and 2 deletions.
  1. +2 −2 lib/rspec/mocks/any_instance/recorder.rb
  2. +20 −0 spec/rspec/mocks/any_instance_spec.rb
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
20 spec/rspec/mocks/any_instance_spec.rb
@@ -845,6 +845,26 @@ 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
+ before do
+ klass.class_eval <<-EOM
+ def method
+ "this is not awesome but does happen in real code"
+ end
+ EOM
+ end
@myronmarston RSpec member

One suggestion -- this doesn't really communicate much about a real situation where #method would be overriden. Instead, you might consider doing something like:

let(:http_request_class) { Struct.new(:method, :uri) }

This communicates better (I think an HTTP request object is the main time you'd see method overriden since it's a proper term in that domain), is less code, and is a bit less "meta" (e.g. no class_eval).

:thumbsup: I like it. I'll change, squash and merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
+ it "stubs the method correctly" do
+ klass.any_instance.stub(:existing_method).and_return("foo")
+ klass.new.existing_method.should == "foo"
+ end
+
+ it "mocks the method correctly" do
+ klass.any_instance.should_receive(:existing_method).and_return("foo")
+ klass.new.existing_method.should == "foo"
+ end
+ end
end
end
end
Something went wrong with that request. Please try again.