RSpec and RR troubles

rspec rr

2009-10-30 19:26:34 +0000

Update: This has to do with Rails and not RSpec or RR

The road to TDD is a dangerous one.

I spent two hours diagnosing an issue that was simple to fix, but a devil to hunt down.

I am using RSpec with RR on a project, and noticed that when I ran my specs with autospec or rake, I had failing tests, but when I ran them individually with spec or textmate, everything was fine.

My first thought was that there was some cross pollination with the mocking and/or database. Well, it turns out, to be much more devious than that.

In another spec, unrelated to the one that was failing, I had the following line:

    UserSession.stubs(:find).returns(@user_session)

Users of rr will notice that this isn’t how you stub in rr, and users of mocha will notice that this is exactly how you stub in mocha.

In the last project I worked on I used mocha, and apparently a bit of that leaked into this project.

The killer is that this line didn’t cause any errors.

Merely making this line rr compatible worked:

    stub(UserSession).find { @user_session }

Update

It appears that mocha is being loaded in this project even thought I don’t have any references to it anywhere.

I opened up a rails console and saw this:

    >> UserSession.stubs(:find).returns(nil)
    => #<Mocha::Expectation:0x103051078 ...>

Where did that come from? Strange… more investigation is necessary

Update #2

Okay, it looks like rails includes mocha if it can find it. This all happens in activesupport’s testcase.rb.

blog comments powered by Disqus