Rails 模型与 RSpec 发生冲突
我正在尝试让 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
::Change
表示顶级命名空间,因为 RSpec 的Change
类位于模块RSpec::Matchers
中。就这样:Use
::Change
to denote the toplevel namespace, since RSpec'sChange
class is within the moduleRSpec::Matchers
. As so: