The Blog

Sep 10
10

Rails 3 + Mocha Load Order Gotcha

by Tom Quackenbush

The other day we ran into an issue in one of our new Rails 3 apps that is using mocha for mocking. It seemed that once a stub was mocked in one test, it would carry over to each subsequent test causing failures.
Mocks were not being torn down correctly…
Since mocha does not lie, it had to be something within our setup causing it to fail…

Thankfully, a tweet response from @elise_huard offered the following solution:

@tquackenbush yes, @threedaymonk suggested a fix: to fix load order, so do gem ‘mocha’, :require => false and require ‘mocha’ in test_helper”

Worked like a charm!
A tip of the hat to @elise_huard and @threedaymonk for the help!

  • Reddit
  • Digg
  • del.icio.us
  • Facebook
  • Tumblr
  • Twitter

Comments

  1. Thanks for this post! I had the same problem and couldn’t figure out what was wrong.

  2. Thanks! You just saved our lives.

  3. [...] This is a documented gotcha with mocha. And also discussed here. [...]

  4. Thanks soo much!!!

    I’ve lost over three hours trying to figure it out why mocha wasn’t avaiable when my tests where running.

    I just did what you said to and it worked perfectly!

  5. that was still failing for us

    what we did is:
    in the test_helper.rb

    def teardown
    super
    Mocha::Mockery.instance.teardown
    Mocha::Mockery.reset_instance
    end

  6. Hey CLod,

    in Rails Versions > 3.1.1 there was added a hack to bring in the turn gem.
    (as you can see here https://github.com/rails/rails/commit/6eff04499e28865890e1ae3915fe80e4903a997b)

    in the activesupport/lib/active_support/testing/mochaing.rb the mocha library is inlcuded seperately, so any “require false” will not help. Mocha will still be required far before the test unit library.

    Turning off “turn” has helped me, but is really sad, i like it ;)
    I know it is probably too late, but maybe you are still interested in.
    Regards, Falk

  7. how do i turn off turn in rails 3.1

  8. Thanks. I know the install instructions said to include the “:require => false”, but I forgot that I didn’t include it. Another project I checked wasn’t doing this, and I assumed it was working fine.

    Also, it’s working with Turn just fine for me right now with Rails 3.2.2.

Leave a Reply

*