PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, May 18, 2022

[FIXED] How to test that a view helper renders a partial?

 May 18, 2022     partial, ruby-on-rails, specs, view-helpers     No comments   

Issue

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?


Solution

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,



Answered By - Novae
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing