Skip to content

stack level too deep after mocking via any_instance on a DataMapper validator #128

Closed
asdavey opened this Issue Apr 26, 2012 · 10 comments

7 participants

@asdavey
asdavey commented Apr 26, 2012

After calling any_instance on a DataMapper validator in one example, the next example to contain a call to DataMapper::Model.valid? will cause a stack nested too deep error.

Not sure if the this is a RSpec or DataMapper issue.

I've included a reproducible case at https://gist.github.com/2496898.

Running rspec stack_error_spec.rb results in:


.F

Failures:

  1) Post should still work after mocks have been removed
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /home/Andrew/.rvm/gems/ruby-1.9.2-p290@x/gems/rspec-mocks-2.9.0/lib/rspec/mocks/space.rb:6

Finished in 0.03504 seconds
2 examples, 1 failure

Failed examples:

rspec ./stack_error_spec.rb:32 # Post should still work after mocks have been removed

I have been able to reproduce the error on ruby-1.8.7, ruby-1.9.2 and jruby-1.6.7

@dchelimsky
RSpec member

I'm guessing this is related to #120 /cc @kaiwren

@asdavey
asdavey commented Apr 26, 2012

Probably. But it doesn't sound like the 'fork bomb' description given to #120. And the error only occurs after the stub should have been torn down.

@voidlily
voidlily commented Jun 4, 2012

I have a similar issue with Sequel. Minimal case: https://gist.github.com/2869129

@torbjoernk

Experiencing this bug while trying to mock writing to a (plaintext) file.

File.should_receive(:open).with(filename, "w")
IO.any_instance.should_receive(:puts).with(expected_content)

Minimal example here: https://gist.github.com/3113509

Tested with current head (f36ad4d) and Ruby 1.9.3-p194.

/edit/
Just to verify: When commenting out IO.any_instance, no error is thrown.

@dchelimsky
RSpec member

@torbjoernk I'm not sure why there's a stack overflow, but what you're trying to do won't work because nil is being yielded to File.open. You need to declare explicitly what should be returned by or yielded from File.open when you use should_receive. Try this instead of what's in your gist:

describe "message expectation on a file" do
  it "works like this" do
    filename = "test_file"
    expected = "this should be written to test_file"
    file = stub
    file.should_receive( :puts ).with( expected )
    File.should_receive( :open ).with( filename, "w" ).and_yield( file )
    File.open( filename, "w" ) do |f|
      f.puts expected
    end
  end
end
@torbjoernk

@dchelimsky Thanks a lot. That worked. Though your code is a little "stuttering": file.should_receive.should_receive() ;) Now I'm going to educate myself about that and_yield ...

@dchelimsky
RSpec member

@torbjoernk fixed the stuttering - thanks!

@kalbasit

I'm experiencing the same issue as @asdavey any spec that comes after the one where I have ObjectName.any_instance.stub is failing..

@alindeman

I feel like this is likely related to an instance being created in one spec that's sticking around in a different spec. That doesn't play nicely with any_instance.

Moving discussion to #167.

@alindeman alindeman closed this Aug 7, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.