【发布时间】:2014-11-05 17:27:48
【问题描述】:
我有一个看起来像这样的类:
class Foo < ActiveRecrod::Base
has_many :bars
def nasty_bars_present?
bars.where(bar_type: "Nasty").any?
end
validate :validate_nasty_bars
def validate_nasty_bars
if nasty_bars_present?
errors.add(:base, :nasty_bars_present)
end
end
end
在测试#nasty_bars_present?我想编写一个 rspec 测试来存根条关联但允许自然执行的位置。比如:
describe "#nasty_bars_present?" do
context "with nasty bars" do
before { foo.stub(:bars).and_return([mock(Bar, bar_type: "Nasty")]) }
it "should return true" do
expect(foo.nasty_bars_present?).to be_true
end
end
end
上面的测试给出了一个关于数组没有方法的错误。如何包装模拟以便正确执行?
谢谢!
【问题讨论】:
-
你使用的是什么版本的 RSpec?
-
这个项目在 2.14.1 上,但我也有兴趣在最新版本上执行。
标签: ruby-on-rails ruby unit-testing rspec