ruby-on-railstestingruby-mocha

Installing Mocha 0.9.7 in a Rails 2.3.3 project


I installed the Mocha 0.9.7 Rails plug-in using:

$ script/plugin install git://github.com/floehopper/mocha.git

(Just followed instruction in http://mocha.rubyforge.org/)

Then, I have the following set-up defined in my functional test

def setup
  @controller.expects(:logged_in?).returns(true)
  @controller.expects(:admin_user?).returns(true)
end

Running the test generates the ff. error:

NameError: uninitialized constant Mocha::Mockery::ImpersonatingName
    /test/functional/xxxx_controller_test.rb:x:in `setup'

Before that, I see the ff. error at the top of the test log:

/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.3/lib/active_support/test_case.rb:12: warning: already initialized constant Mocha

This has led me to believe that I have an old version of Mocha somewhere in Ruby's or Rails' path. The problem is I can't find it.

Is my guess correct? If so, where is this old version of Mocha? Alternatively, how can I found out where it is?


Solution

  • According to http://selfamusementpark.com/blog/2009/07/30/rails233mochaconfusion/, my guess is not correct. The problem really is that Mocha is being loaded before the testing framework which is not what the former expects. The solution is to edit RAILS_ROOT/vendor/plugin/mocha/init.rb to comment out the ff. line:

    require 'mocha'
    

    Then, Mocha will have to be explicitly required in the test files or helpers to ensure that the testing framework will have been loaded beforehand.