0

尝试为使用acts_as_tree 的辅助方法编写测试。

辅助方法对子项使用计数:

if category.children.size > 0

在我的测试中,我正在创建一个父母和孩子:

parent = Factory.create(:category)
child = Factory.create(:category, :parent_id => parent.id)

但是在我的测试中(以及在测试运行时在我的助手中), parent.child.size 为 0

我的助手规范不能使用acts_as_tree 是否有一些限制?我可以以某种方式包含它吗?还是我应该以某种方式存根?

谢谢!

4

1 回答 1

1

RSpec's stub_chain can deal with situations where you need to stub a chain of method calls on an object. The syntax is slightly different in helper specs vs. view specs:

# helper
helper.stub_chain(:category,:children,:size,:>).with(0) { true }

# view
view.stub_chain(:category,:children,:size,:>).with(0) { true }
于 2011-02-16T23:14:10.223 回答