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

SystemStackError: stack level too deep when using "and_call_original" for overwritten ".new"

Open MmKolodziej opened this issue 5 years ago • 11 comments

Subject of the issue

When overwriting a class' .new method in order to return a subclass and expecting that method to be called with .and_call_original, we end up with a stack level too deep.

Your environment

  • Ruby version: 2.6.5
  • rspec-mocks version: 3.9.1

Steps to reproduce

begin
  require "bundler/inline"
rescue LoadError => e
  $stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
  raise e
end

gemfile(true) do
  source "https://rubygems.org"

  gem "rspec", "3.9.0" # Activate the gem and version you are reporting the issue against.
end

require 'rspec/autorun'

class A
  def self.new
    return super if self == B
    B.new
  end
end

B = Class.new(A)

RSpec.describe A do
  it do
    expect(A).to receive(:new).and_call_original
    A.new
  end
end

Expected behavior

To be fair, I realize it's far from being a common use case. I'd understand if there was no desire to fix it, as fixing it could potentially mean treating stubbing .new in a different way.

Actual behavior

SystemStackError:
  stack level too deep

MmKolodziej avatar Mar 14 '20 18:03 MmKolodziej