Mocha 的 regexp_matches 有时会丢失

发布于 2024-11-25 22:21:27 字数 1151 浏览 3 评论 0原文

我很难从 Mocha 和 regexp_matches 方法。如果自动测试运行我的整个测试套件,则一切正常。如果我故意导致包含 regexp_matches 调用的测试失败,然后修复它,我会在 regexp_matches 上收到 method_missing 错误。如果我再次运行整个测试套件,一切都会很好。更大的问题来自 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' 但这并没有改变任何东西。

I'm having a hard time getting any consistant behavior from Mocha and the regexp_matches method. If autotest runs my entire test suite everything works fine. If I purposely cause the test containing the regexp_matches call to fail and then fix it I get a method_missing error on regexp_matches. If I then run the entire test suite again, everything is fine. The bigger problem is coming from Hudson (continuous integration). It runs the entire test suite but always says regexp_matches is missing and I don't know how to fix it.

My test:

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

The error:

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

I don't even know what other code to add here as I can't imagine what the cause is. For giggles I pasted require 'mocha' at the top of the test file but that didn't change anything.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把人绕傻吧 2024-12-02 22:21:28

我在删除 Rails 项目时遇到了这个问题,

require 'spec_helper'

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

但即使在指定之后

require 'mocha'

我也遇到了相同的方法丢失问题。

最终,我通过直接包含参数匹配器模块解决了这个问题:

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

include Mocha::ParameterMatchers

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

I ran into this problem on a rails project when removing

require 'spec_helper'

I did this so that running the spec would not load the whole rails environment. This means that external dependencies have to be required or mocked. Mocha obviously needs to be required.

But even after specifying

require 'mocha'

I ran into the same method missing issue.

Ultimately, I solved it by including the parameter matchers module directly:

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

include Mocha::ParameterMatchers

describe SomeClass do
  it "should do things" do
  ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文