14km

Posts tagged mocha

May 27

Mocha doesn’t play nice with Rails 3 & Bundler

Using mocha with edge beta Rails 3, I added Mocha to the Gemfile

gem "mocha"

All was going swimmingly until I ran into a problem. I reduced it to:

The result of running this test was:

#test_mock_doesnt_fail should fail, but doesn’t. Changing the require lines at the top of the file to not load Rails and only load Mocha (+Rubygems+Test::Unit):

require "rubygems"
require "test/unit"
require "mocha"

caused the test to fail as expected. After an hour or two digging around, I stumbled across a post on the Mocha mailing-list that mentioned ordering problems with Mocha and Test::Unit. Sure enough, if I rearranged the modified require lines so that Test::Unit was loaded after Mocha:

require "rubygems"
require "mocha"
require "test/unit"

the test would break — I.e. wouldn’t fail.

Following the advice in the post — modified for bundler — I changed my Gemfile to read:

gem 'mocha', :require => nil

and added:

require "mocha"

to test/test_helper.rb to make sure Mocha was loaded. This isn’t strictly needed, but it’s good to be clear.

Final result, the test fails as expected, and Mocha is seems to be working properly.