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

1

6

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.

Tilden answered 30/9, 2011 at 10:46 Comment(0)
T
11

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!

Tilden answered 30/9, 2011 at 10:57 Comment(1)
However, I still can't use mock() in the rails console without it complaining, even if I load the test environment. Annoying.Tilden

© 2022 - 2024 — McMap. All rights reserved.