Say I want to render different partials depending on an instance variable from the controller. I put the logic in a helper method, that looks something like:
def display_my_partial(foo)
foo == bar ? render(partial_x) : render(partial_y)
end
and in the view I call (using Slim):
= display_my_partial(@foo)
What should my test look like? I tried something like:
expect(display_my_partial(foo)).to render(partial: 'partial_x')
but got:
NoMethodError:
undefined method `matches?' for #<ActiveSupport::SafeBuffer:0x007ffb490aba80>
My example is a bit more complicated, as my partials are in a nested namespace. I had to experiment a little with just usind render 'partial_x'
vs render partial: 'namespace/model/partial_x'
to get it working in the specs, but finally I got the above mentioned error.
So how would you test this?
Where are you testing it in? Make sure render_views is called.
In any case, do you really care it's rendering that partial? What if the file name is changed, or you decide to change the implementation using html helpers instead. None of this matters to the output. I would personally assert the output instead. Depending on how complex the output is you could do it in a view test or just simple unit tests.
HTH,