Mocha的regexp_matches有时会丢失

时间:2011-07-25 20:46:14

标签: ruby-on-rails ruby-on-rails-3 hudson autotest mocha

我很难从Mocha和regexp_matches方法获得任何一致的行为。如果autotest运行我的整个测试套件一切正常。如果我故意导致包含regexp_matches调用的测试失败然后修复它,我会在method_missing上收到regexp_matches错误。如果我再次运行整个测试套件,一切都很好。更大的问题来自Hudson(持续整合)。它运行整个测试套件,但总是说regexp_matches缺失,我不知道如何解决它。

我的测试:

test "if token is set during Account creation the long url should be created correctly" do
  Account.any_instance.expects(:http_get).with("api.server.com", regexp_matches(%r(^http://.*/accounts/\d+/jobs$)))
  account = Account.create name: "New Account", token: "NewToken"
end

错误:

test_if_token_is_set_during_Account_creation_the_long_url_should_be_created_correctly(AccountTest):
NoMethodError: undefined method `regexp_matches' for #<AccountTest:0x0000010162d0c0>
    test/unit/account_test.rb:158:in `block in <class:AccountTest>'

我甚至不知道在这里添加什么其他代码,因为我无法想象原因是什么。对于咯咯笑声,我将require 'mocha'粘贴在测试文件的顶部,但没有改变任何内容。

1 个答案:

答案 0 :(得分:1)

我在删除

时在rails项目上遇到了这个问题
require 'spec_helper'

我这样做是为了运行规范不会加载整个rails环境。这意味着必须要求或模拟外部依赖项。显然需要摩卡。

但即使在指定

之后
require 'mocha'

我遇到了同样缺少问题的方法。

最终,我通过直接包含参数matchers模块来解决它:

require_relative "../../../lib/some_class"
require "mocha"

include Mocha::ParameterMatchers

describe SomeClass do
  it "should do things" do
  ...