我想写一个范围,要求产品的开始日期少于今天。我在rspec中写了以下内容
it "Should not be found with a start date in the future" do
@product.start_date = Date.tomorrow
@product.save
Product.active.find(@product.id).should == nil
end
显然,该测试失败了。然后我写了范围 -
scope :active, where('start_date <= ?', Date.today)
然后我重新运行规范,它失败了 -
2) Product Should not be found with a start date in the future
Failure/Error: Product.active.find(@product.id).should_not == true
Couldn't find Product with ID=1 [WHERE (start_date <= '2010-12-20')]
# ./spec/models/product_spec.rb:168:in `block (2 levels) in <top (required)>'
我无法弄清楚如何让这段代码通过。我不希望找到该产品。
答案 0 :(得分:3)
查看错误:“无法找到ID = 1的产品”,范围实际上正在运行。问题出在你的测试中,find
像往常一样引发异常,因为没有找到记录。您必须使用find_by_id
或使用rspec声明该异常。