rspec-mocks icon indicating copy to clipboard operation
rspec-mocks copied to clipboard

`stack level too deep` when stubbing :is_a? with allow_any_instance_of

Open suzukimilanpaak opened this issue 6 months ago • 0 comments

Subject of the issue

The error "stack level too deep" occurs when stubbing :is_a? with allow_any_instance_of.

Your environment

  • Ruby version: ruby 2.7.5p203 (2021-11-24 revision f69aeb8314) [x86_64-linux]

  • rspec-mocks version: 3.12.6

Steps to reproduce

stub_is_a_spec.rb
describe '`stack level too deep` when stubbing :is_a? ' do
  let(:klass) do
    Class.new do
      def a
        is_a?(self)
      end
    end
  end

  before { allow_any_instance_of(klass).to receive(:is_a?).and_return(true) }

  it { expect(klass.new.a).to be(true) }
end

Expected behavior

The spec should pass.

Actual behavior

% rspec stub_is_a_spec.rb
Failures:

  1) AccountsProduction::AppSwitcherRequester 
     Failure/Error: is_a?(self)
     
     SystemStackError:
       stack level too deep

The stack is recursively called at this line of code obviously. https://github.com/rspec/rspec-mocks/blob/v3.12.6/lib/rspec/mocks/proxy.rb#L38

I don't encounter the error if I initialize the class and utilize the instance generated.

suzukimilanpaak avatar Jan 17 '24 14:01 suzukimilanpaak