开发者

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/ass开发者_StackOverflowertions/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.


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜