Search code examples
ruby-on-railsrspecacts-as-tree

Rspec with acts_as_tree: Helper does not see children


Trying to write a test for a helper method that uses acts_as_tree.

Helper method uses a count for children:

if category.children.size > 0

In my test, I'm creating a parent and child:

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

But in my test (and in my helper when run by the test), parent.child.size is 0

Is there some limitation that my helper spec can't use acts_as_tree? Can I include it somehow? Or should I be stubbing that somehow?

Thanks!


Solution

  • 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 }