Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Illustrate bug with current ancestor approach #258

Closed
wants to merge 1 commit into from

3 participants

@marcandre

Here is a test that fails.
I didn't check extensively how to fix it. I'm not convinced the current approach is the best one. Has it been attempted to use super from the mocked method, for example if @rspec_call_original is set?

@marcandre

Note: Not a problem in Ruby 2.1 because of the consistent ancestors list (assuming #257 is merged).

@myronmarston

Thanks, I'll have to dig in to it.

@marcandre

BTW, the reason why this fails is that the current handling goes through the ancestors list first (but Ruby 2.0- hides the singleton classes), then checks the superclass if there is one (because it is hidden from the ancestors list).

The test is an example where the superclass actually comes before the module found in ancestors. You can't check the superclass first either, because in other cases the first definition would be in an included module...

@myronmarston myronmarston referenced this pull request from a commit
@myronmarston myronmarston Candidate fix for #258. 03f0de1
@myronmarston myronmarston referenced this pull request
Merged

Candidate fix for #258 #264

@myronmarston myronmarston referenced this pull request from a commit
@myronmarston myronmarston Candidate fix for #258. 996c705
@JonRowe
Owner

Did we fix this?

@myronmarston
Owner

I still need to wrap up the candidate fix. Thanks for reminding me. Too much to do, too little time....

@JonRowe
Owner
@myronmarston
Owner

Milestone has been cleared :).

We may consider backporting this to a 2.14.x release, though, but I'll wait to decide that.

@myronmarston myronmarston referenced this pull request from a commit
@myronmarston myronmarston Candidate fix for #258. c864e0a
@myronmarston

I just merged a fix for this in #264 that also greatly simplifies the logic. The fix will be in RSpec 3.

Thanks for reporting this and the clear failure example, @marcandre!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 30, 2013
  1. @marcandre
This page is out of date. Refresh to see the latest.
Showing with 9 additions and 0 deletions.
  1. +9 −0 spec/rspec/mocks/and_call_original_spec.rb
View
9 spec/rspec/mocks/and_call_original_spec.rb
@@ -77,6 +77,15 @@ def instance.foo; :bar; end
expect(sub_klass.foo).to eq(:sub_klass_bar)
end
+ it "finds the method on the most direct singleton class ancestors even if the method " +
+ "is available on more distant ancestors" do
+ klass.extend Module.new { def foo; :klass_bar; end }
+ sub_klass = Class.new(klass) { def self.foo; :sub_klass_bar; end }
+ sub_sub_klass = Class.new(sub_klass)
+ sub_sub_klass.should_receive(:foo).and_call_original
+ expect(sub_sub_klass.foo).to eq(:sub_klass_bar)
+ end
+
context 'when using any_instance' do
it 'works for instance methods defined on the class' do
klass.any_instance.should_receive(:meth_1).and_call_original
Something went wrong with that request. Please try again.