摩卡小样进行另一项测试 [英] Mocha Mock Carries To Another Test

查看:76
本文介绍了摩卡小样进行另一项测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在关注

I have been following the 15 TDD steps to create a Rails application guide - but have run into an issue I cannot seem to resolve. For the functional test of the WordsController, I have the following code:

class WordsControllerTest < ActionController::TestCase

  test "should get learn" do
    get 'learn'
    assert_response :success
  end

  test "learn passes a random word" do    
    some_word = Word.new
    Word.expects(:random).returns(some_word)
    get 'learn'
    assert_equal some_word, assigns('word')
  end
end

在Word类中,我有以下代码:

In the Word class I have the following code:

class Word < ActiveRecord::Base
  def self.random
    all = Word.find :all
    all[rand(all.size)]
  end
end

运行测试时,我遇到以下错误(为简便起见,简称以下错误):

When I run the tests, I experience the following error (shortened for brevity):

1) Failure: unexpected invocation: Word(...).random() satisfied expectations:
- expected exactly once, already invoked once: Word(...).random()

我尝试更改测试顺序以及其他许多事情,但是一次又一次,我继续收到相同的测试失败-已经调用了Word.random().

I have tried changing changing the order of the tests along with a multitude of other things, but time and time again I continue to receive the same test failure - that Word.random() has already been invoked.

我正在运行Rails 3.0 beta 4和Mocha 0.9.8.我已经为寻找问题的解决方法进行了漫长而艰苦的搜索,但似乎找不到它.我是Ruby/Rails的新手,所以对语言和框架不熟悉.

I'm running Rails 3.0 beta 4 and Mocha 0.9.8. I've searched long and hard for a solution to my problem, but I can't seem to find it. I'm new to Ruby/Rails so am rather unfamiliar with the language and the frameworks.

提前谢谢!

推荐答案

我遇到了同样的问题,模拟功能并未隔离到测试中,这似乎与Mocha的加载顺序有关.

I had the same problem, mocked functionality was not isolated to a test, it seems to be a problem with the load order of Mocha.

我在使Mocha与Rails3配合使用时遇到一些问题.我发现了一些关于stackoverflow的帖子,但是直到在agoragames.com上找到帖子后,才偶然发现解决方案.

I had some issues getting Mocha to work with Rails3. I found a few stackoverflow posts regarding, but didn't stumble across the solution until I found a post on agoragames.com

基本上,在您的项目的Gemfile中,对Mocha的需求应类似于:

Basically, in the Gemfile of your project, the require for Mocha should look like:

gem 'mocha', :require => false

然后在test/test_helper.rb中,为摩卡咖啡添加一条要求行:

Then in test/test_helper.rb, add a require line for mocha:

...
...
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'mocha'

class ActiveSupport::TestCase
...
...

我认为Gemfile中的mocha要求行意味着您需要已经将mocha作为gem安装,捆绑程序不会为您处理它.

I think the require line for mocha in the Gemfile means that you need to already have mocha installed as a gem, bundler won't take care of it for you.

这篇关于摩卡小样进行另一项测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆