Skip to content


Subversion checkout URL

You can clone with
Download ZIP


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

asdavey opened this Issue · 10 comments

7 participants


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

Running rspec stack_error_spec.rb results in:



  1) Post should still work after mocks have been removed
     Failure/Error: Unable to find matching line from backtrace
       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


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


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.


I have a similar issue with Sequel. Minimal case:


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

File.should_receive(:open).with(filename, "w")

Minimal example here:

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

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


@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 You need to declare explicitly what should be returned by or yielded from 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 ) filename, "w" ) do |f|
      f.puts expected

@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 ...


@torbjoernk fixed the stuttering - thanks!


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


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
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.