我正在将 rails 2 应用程序升级到 rails 3 并且不确定如何从我的一个测试中“升级”以下行
Category.should_receive(:find).with(:all,:conditions => {:parent_id => @parent_id}, :order => 'disp_order DESC').and_return(@categories_collection)
希望有人可以提供一些指示,因为我不是 100% 确定从哪里开始。
运行此程序时出现以下错误:
Failure/Error: Category.should_receive(:find).with(:all,
(<Category(id: integer, permalink: string, name: string, parent_id: integer) (class)>).find(:all, {:conditions=>{:parent_id=>1}, :order=>"display_order DESC"})
* *更新 1
我现在看到一些非常奇怪的东西,我已经按照 Jim 的解释进行了重构(顺便说一句很好的解释!),但现在得到以下结果:
Failure/Error: Category.should_receive(:with_parent).with(1).and_return(@sub_category)
(<Category(id: integer, permalink: string, name: string, parent_id: integer) (class)>).with_parent(1)
expected: 1 time
received: 0 times
但是,如果我将以下内容添加到我的测试中:
puts Category.with_parent(1).length.to_s
输出为“1” - 正确/预期值。由于某种原因,RSpec 没有看到这个并抛出错误。你知道为什么会发生这种情况吗?
* *更新 2
好的,有趣的是,如果我使用以下测试通过:
Category.with_parent(@parent_id).should == [@sub_category]
虽然这失败了:
Category.should_receive(:with_parent).with(@parent_id).and_return(@sub_category)
在 rspec2 的上下文中使用 should_receive & .ad_return 有问题吗?