ruby-on-railsruby-on-rails-3mockingruby-mocha

NoMethodError: undefined method `mock' with Mocha and Rails 3


I'm trying to use mocha in my Rails 3 project but keep getting the following exception:

NoMethodError: undefined method `mock' for #<MochaTest:0x00000101f434e8>
    /Users/John/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.0.10/lib/action_dispatch/testing/assertions/routing.rb:175:in `method_missing'
    test/functional/mocha_test.rb:7:in `block in <class:MochaTest>'

Test

I've written the simplest test possible:

require 'test_helper'

class MochaTest < ActionController::TestCase
  test "test mocha" do
    order = mock('order')
  end
end

I run it using ruby -Itest test/functional/mocha_test.rb

I've tried rake test and it gives exactly the same exception.

test_helper.rb

ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'

class ActiveSupport::TestCase
  #....
  require 'test/unit'
  require 'mocha' 
end

GemFile

source 'http://rubygems.org'

gem 'rails', '3.0.10'
gem 'sqlite3'
gem 'devise', '1.4.5'
gem 'activemerchant'
gem 'geo_location'
gem 'nokogiri'
gem "nifty-generators", :group => :development
gem 'mocha', '0.10.0'

Things I've Tried

I'm tearing my hair out with this. Any ideas would be gratefully received.


Solution

  • I found the answer immediately after I'd posted this.

    Here's the answer: Mocha Mock Carries To Another Test

    In short, I changed

    gem 'mocha', '0.10.0'

    to

    gem 'mocha', '0.10.0', :require => false

    and it worked like a charm!