I just updated a project to Rails 2.3.3 for the cool new “touch” feature, and saw here that I needed to update to Mocha 0.9.7. I did that, but found that my tests were failing - I got lots of NoMethodError: undefined method 'stub' for #<SomeTest:0x7f2c1d921f80> errors.

In my project, I’d declared my dependence on the Mocha gem in my config/environment.rb file, so that sudo rake gems:install would load everything required for development in one shot (which is why the dependency isn’t in config/environments/test.rb). I’ll cut to the chase: I needed to add an extra :lib option to that declaration:

config.gem 'mocha', :version => '=0.9.7', :lib => false

The problem is that mocha configures itself based on what test library you’ve already included when you require Mocha. Unfortunately, the config.gem declaration causes mocha to be loaded before Rails has loaded Test::Unit, so Mocha doesn’t configure itself… so no “stub.” Setting :lib to false postpones loading Mocha until you actually do it in your test/test_helper.rb (or wherever).