Getting Rails 2.3.3 and Mocha >=0.9.6 to play nice with Test::Unit and Shoulda

Simultaneous changes in Rails 2.3.3 and Mocha >=0.9.6 have caused some headaches in test::unit (or shoulda) tests. In my case I saw anomalies such as expectations persisting across tests. You put expects(:foo).never in test A and get “unsatisfied expectation” failure in test B where you happen to call :foo. This is due to unfortunate dependency on load order which could cause runner logic to be erroneously overridden. Here’s the steps you need to take to tidy up this setup to make sure it works correctly.

Update mocha to >= 0.9.7

  ~% sudo gem update mocha

Install test-unit gem

  ~% sudo gem install test-unit

Don’t load mocha yourself

If you had anything like this line in your test environment, REMOVE IT.

config.gem "mocha"

Ensure correct load order

If using Shoulda, make sure your test.rb environment loads test-unit before shoulda.

  config.gem "test-unit", :lib => "test/unit", :version => ">=2.0.3"
  config.gem "thoughtbot-shoulda", :lib => "shoulda", :source => "http://gems.github.com", :version => ">=2.10.1"

This should hopefully fix mocha issues with rails 2.3.3.

For more explanation check out this message on mocha’s mail list and this lighthouse ticket.

2 responses to “Getting Rails 2.3.3 and Mocha >=0.9.6 to play nice with Test::Unit and Shoulda”

  1. Steve Madsen

    Hi, Maxim. It appears that simply ensuring that mocha comes after shoulda in test.rb is sufficient for me. I was seeing similar “bleed through” of expectations between test cases and this fixed the problem for me, without having to install a new version of Test::Unit. I am also running with Rails 2.3.4, though.

Leave a Reply