Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

  • Installing mocha 0.9.5 after reading 0.9.5-7 had issues with this. I get an undefined method name exception instead
  • Changing where I require mocha - at the bottom of test_helper.rb, top of test etc.
  • Tried calling mock() in rails console test - I get the same exception

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

like image 633
John Gallagher Avatar asked Sep 30 '11 10:09

John Gallagher


1 Answers

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!

like image 61
John Gallagher Avatar answered Sep 28 '22 07:09

John Gallagher