Skip to content

Stop inherited method's unrecoverble stub

Created by: moro

Stubbing inherited method with any_instance sometimes cause SystemStackError, below is reproduction example.

class SuperClass
  def foo; end
end

class SubClass < SuperClass
end

describe SubClass do
  it "workds fine at firstt" do
    SuperClass.any_instance.stub(:foo)
    SubClass.any_instance.stub(:foo)
  end

  it "cause SystemStackError after removing stub" do
    SubClass.new.foo # => SystemStackError raised
  end
end

restore_original_method for SubClass restores SuperClass's stubbed method, so that SubClass#foo is broken.

This commit prevent this problem by raising error immediately when SubClass's any_instance stub is defined.

Merge request reports