Rails 模型与 RSpec 发生冲突

发布于 2024-10-27 20:07:52 字数 918 浏览 0 评论 0原文

我正在尝试让 Rails 应用程序的 RSpec 测试恢复工作。据我所知,它们绿色时和现在之间唯一真正的区别是我现在使用的是 ruby​​ 1.9,而它们过去使用的是 ruby​​ 1.8.7。

模型

class Change < ActiveRecord::Base
...
end

我有一个使用规范的

describe ChangeObserver do
  let (:c) { Change.new(:comment => "Test", :originator => "x.y")}
  it "finds affected modules for a change" do
    c.should_receive(:affected).and_return([])
    c.save
  end
end

:(是的,我需要一个 Change 实例来测试观察者)。

这些规范失败了:

1) ChangeObserver finds affected modules for a change
   Failure/Error: c.save
   NoMethodError:
     undefined method `save' for #<RSpec::Matchers::Change:0x3c8e5f0>

所以显然我的 Change 类与 [RSpec::Matchers::Change][1] 冲突,但它并没有一直这样做(我确信它可以与 ruby​​ 1.8.7 一起使用)。 1.9 中 ruby​​ 加载模块的方式有什么不同吗?我如何require我自己的Change类(注意:它不在模块内,所以我不知道如何限定它)。

I'm trying to get the RSpec-tests of my Rails application back to work. As far as I can tell, the only real difference between when they were green and now is that I'm now using ruby 1.9, whereas they used to pass with ruby 1.8.7.

I have a model

class Change < ActiveRecord::Base
...
end

which is used a spec:

describe ChangeObserver do
  let (:c) { Change.new(:comment => "Test", :originator => "x.y")}
  it "finds affected modules for a change" do
    c.should_receive(:affected).and_return([])
    c.save
  end
end

(yes, I need a Change instance for testing the observer).

These specs fail with:

1) ChangeObserver finds affected modules for a change
   Failure/Error: c.save
   NoMethodError:
     undefined method `save' for #<RSpec::Matchers::Change:0x3c8e5f0>

So obviously my Change class clashes with [RSpec::Matchers::Change][1], but it didn't do that all the time (I am sure it worked with ruby 1.8.7). Is there something different to the way ruby loads modules in 1.9? How can I require my own Change class (note: it is not inside a module, so I don't know how to qualify it).

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

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

发布评论

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

评论(1

落花浅忆 2024-11-03 20:07:52

使用 ::Change 表示顶级命名空间,因为 RSpec 的 Change 类位于模块 RSpec::Matchers 中。就这样:

describe ChangeObserver do
  let (:c) { ::Change.new(:comment => "Test", :originator => "x.y")}
  it "finds affected modules for a change" do
    c.should_receive(:affected).and_return([])
    c.save
  end
end

Use ::Change to denote the toplevel namespace, since RSpec's Change class is within the module RSpec::Matchers. As so:

describe ChangeObserver do
  let (:c) { ::Change.new(:comment => "Test", :originator => "x.y")}
  it "finds affected modules for a change" do
    c.should_receive(:affected).and_return([])
    c.save
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文