gpt4 book ai didi

ruby-on-rails - 无方法错误 : undefined method `mock' with Mocha and Rails 3

转载 作者:行者123 更新时间:2023-11-28 19:48:25 25 4
gpt4 key购买 nike

我正在尝试在我的 Rails 3 项目中使用 mocha,但不断出现以下异常:

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>'

测试

我写了最简单的测试:

require 'test_helper'

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

我使用 ruby -Itest test/functional/mocha_test.rb 运行它

我试过 rake test 并且它给出了完全相同的异常。

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

gem 文件

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'

我尝试过的事情

  • 在阅读 0.9.5-7 之后安装 mocha 0.9.5 遇到了这个问题。我得到了一个 undefined method name 异常
  • 更改我需要 mocha 的位置 - 在 test_helper.rb 的底部、测试顶部等。
  • 尝试在 Rails 控制台测试中调用 mock() - 我得到了同样的异常

我被这个搞得焦头烂额了。任何想法都将不胜感激。

最佳答案

发布后我立即找到了答案。

这是答案:Mocha Mock Carries To Another Test

总之,我变了

gem 'mocha', '0.10.0'

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

它就像一个魅力!

关于ruby-on-rails - 无方法错误 : undefined method `mock' with Mocha and Rails 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7609292/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com