Skip to content

Commit

Permalink
Add reproduction spec for stack overflow in error formatting
Browse files Browse the repository at this point in the history
as initially reported in rspec/rspec-rails#2049

The issue occurs when a class defines an `inspect` method which depends on
methods that have been stubbed to e.g. `receive(...).at_most(n).times`.
Failures try to call `inspect` which fails which calls `inspect` which ...
  • Loading branch information
jamesdabbs-procore committed May 3, 2019
1 parent 55f0b11 commit 68e5f18
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions spec/rspec/mocks/partial_double_spec.rb
Expand Up @@ -152,6 +152,34 @@ def self.inspect
}.to fail_with(/MyClass/)
end

it "formats an error message when inspect fails" do
klass = Struct.new(:name) do
def inspect
"<Inspector(#{name})>"
end
end

object = klass.new('foo')

expect(object).to receive(:name).once.and_return('bar')

object.name

expect do
object.name
end.to raise_error(
RSpec::Mocks::MockExpectationError,
%|(<Inspector(bar)>).name(no args)\n expected: 1 time with any arguments\n received: 2 times|
)

expect do
verify object
end.to raise_error(
RSpec::Mocks::MockExpectationError,
%|(<Inspector(bar)>).name(*(any args))\n expected: 1 time with any arguments\n received: 2 times with any arguments|
)
end

it "shares message expectations with clone" do
expect(object).to receive(:foobar)
twin = object.clone
Expand Down

0 comments on commit 68e5f18

Please sign in to comment.