【问题标题】:Rspec with acts_as_tree: Helper does not see children带有acts_as_tree的Rspec:助手看不到孩子
【发布时间】:2011-06-28 12:06:52
【问题描述】:

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

Helper 方法对孩子使用计数:

if category.children.size > 0

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

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

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

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

谢谢!

【问题讨论】:

    标签: ruby-on-rails rspec acts-as-tree


    【解决方案1】:

    RSpec 的stub_chain 可以处理需要存根对象上的方法调用链的情况。帮助规范与视图规范的语法略有不同:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-06
      • 2016-05-12
      • 1970-01-01
      • 2012-05-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多