Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stack level too deep when stubbing :is_a? with allow_any_instance_of #1566

Open
suzukimilanpaak opened this issue Jan 17, 2024 · 0 comments
Open

Comments

@suzukimilanpaak
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant