Skip to content
This repository

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

Closed
wants to merge 1 commit into from

2 participants

Andy Lindeman Myron Marston
Andy Lindeman
Owner
Myron Marston
Owner

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.

Myron Marston myronmarston commented on the diff August 25, 2012
spec/rspec/mocks/any_instance_spec.rb
@@ -845,6 +845,26 @@ class RSpec::SampleRspecTestClass;end
845 845
           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}")
846 846
         end
847 847
       end
  848
+
  849
+      context "when a class overrides Object#method" do
  850
+        before do
  851
+          klass.class_eval <<-EOM
  852
+            def method
  853
+              "this is not awesome but does happen in real code"
  854
+            end
  855
+          EOM
  856
+        end
2
Myron Marston Owner

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).

Andy Lindeman Owner
alindeman added a note August 25, 2012

: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
Andy Lindeman alindeman closed this in 645b158 August 25, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Aug 25, 2012
Andy Lindeman any_instance is supported when a class overrides Object#method
* Fixes #180
ea5b749
This page is out of date. Refresh to see the latest.
4  lib/rspec/mocks/any_instance/recorder.rb
@@ -175,7 +175,7 @@ def observe!(method_name)
175 175
           backup_method!(method_name)
176 176
           @klass.class_eval(<<-EOM, __FILE__, __LINE__)
177 177
             def #{method_name}(*args, &blk)
178  
-              klass = self.method(:#{method_name}).owner
  178
+              klass = ::Object.instance_method(:method).bind(self).call(:#{method_name}).owner
179 179
               klass.__recorder.playback!(self, :#{method_name})
180 180
               self.__send__(:#{method_name}, *args, &blk)
181 181
             end
@@ -187,7 +187,7 @@ def mark_invoked!(method_name)
187 187
           @klass.class_eval(<<-EOM, __FILE__, __LINE__)
188 188
             def #{method_name}(*args, &blk)
189 189
               method_name = :#{method_name}
190  
-              klass = self.method(:#{method_name}).owner
  190
+              klass = ::Object.instance_method(:method).bind(self).call(:#{method_name}).owner
191 191
               invoked_instance = klass.__recorder.instance_that_received(method_name)
192 192
               raise RSpec::Mocks::MockExpectationError, "The message '#{method_name}' was received by \#{self.inspect} but has already been received by \#{invoked_instance}"
193 193
             end
20  spec/rspec/mocks/any_instance_spec.rb
@@ -845,6 +845,26 @@ class RSpec::SampleRspecTestClass;end
845 845
           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}")
846 846
         end
847 847
       end
  848
+
  849
+      context "when a class overrides Object#method" do
  850
+        before do
  851
+          klass.class_eval <<-EOM
  852
+            def method
  853
+              "this is not awesome but does happen in real code"
  854
+            end
  855
+          EOM
  856
+        end
  857
+
  858
+        it "stubs the method correctly" do
  859
+          klass.any_instance.stub(:existing_method).and_return("foo")
  860
+          klass.new.existing_method.should == "foo"
  861
+        end
  862
+
  863
+        it "mocks the method correctly" do
  864
+          klass.any_instance.should_receive(:existing_method).and_return("foo")
  865
+          klass.new.existing_method.should == "foo"
  866
+        end
  867
+      end
848 868
     end
849 869
   end
850 870
 end
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.