Missing template in helper specs with a render

I have a helper method that does a “render :partial”. The method works
fine
within the app (Rails 2.3.2). In rspec (1.2.6) I get an error (“Missing
template /comments/_comment.erb in view path”
It seems that rspec when running helper tests doesn’t set the view_path
so
rails doens’t know where to look for the templates. Is there a way to
run
helper specs with the same setup as controller specs?

Charlie

On Thu, Jun 4, 2009 at 8:41 PM, Charlie B.
[email protected] wrote:

I have a helper method that does a “render :partial”. The method works fine
within the app (Rails 2.3.2). In rspec (1.2.6) I get an error (“Missing
template /comments/_comment.erb in view path”
It seems that rspec when running helper tests doesn’t set the view_path so
rails doens’t know where to look for the templates. Is there a way to run
helper specs with the same setup as controller specs?

There is no support for rendering in helper specs as of yet. Please
file a feature request if you think there should be. I have,
personally, never rendered from a helper. Anybody else?

On Fri, 2009-06-05 at 22:40 -0500, David C. wrote:

There is no support for rendering in helper specs as of yet. Please
file a feature request if you think there should be. I have,
personally, never rendered from a helper. Anybody else?

We do this for example to render a set of unrelated items into a single
timeline-based view. The helper sorts out the ordering and renders each
object in it using a specific partial.

Hans

I consider an if statement in the view layer a bug. I often need to
conditionally render a partial and a helper is a great way to
construct that condition. Im currently stubing the call to render but
I only like stub when absolutely necessary.

Sent from my iPhone

On 6 Jun 2009, at 04:40, David C. wrote:

to run
helper specs with the same setup as controller specs?

There is no support for rendering in helper specs as of yet. Please
file a feature request if you think there should be. I have,
personally, never rendered from a helper. Anybody else?

Yep, I quite often have helper methods that wrap a call to
render_partial, but they’d just get tested as part of the main view
render, or by the cukes.

Matt W.
http://blog.mattwynne.net

Sent from my iPhone

On Jun 6, 2009, at 10:02 PM, Zach D. [email protected] wrote:

On Sat, Jun 6, 2009 at 6:34 PM, Charlie B.<[email protected]

wrote:
I consider an if statement in the view layer a bug.

Perhaps we can consider it a possible code smell? It’s not really a
bug unless it’s producing incorrect or unexpected result in the
application’s behaviour.

Absolutely true. Not really a bug but I find it helpful to treat it as
such.

implementation, although my goal isn’t to move every conditional out

<% if current_user.admin? %>
Foo bar baz
<% end

def foo_message
“foo bar baz” if current_user.admin?
end

That’s how I handle that. If for no other reason but easier testing.

On Sat, Jun 6, 2009 at 6:34 PM, Charlie B.[email protected]
wrote:

I consider an if statement in the view layer a bug.

Perhaps we can consider it a possible code smell? It’s not really a
bug unless it’s producing incorrect or unexpected result in the
application’s behaviour.

I often need to
conditionally render a partial and a helper is a great way to construct that
condition. Im currently stubing the call to render but I only like stub
when absolutely necessary.

Helpers can be a great way to organize some of the view’s
implementation, although my goal isn’t to move every conditional out
of the view, it’s to avoid having unnecessary logic in the view. When
I work outside-in I’m able to push logic that doesn’t belong in the
view into a presenter or further down to a model with a good method
name. The logic that is left in the view is very minimal and
ultra-simple. I find it easy to spec and it doesn’t impede the life or
maintainability of the view.

For example, if I need to display a piece of information for an admin,
but not a normal user then I have no problem doing the “if
current_user.admin?” check in a view:

<% if current_user.admin? %>
Foo bar baz
<% end %>

The check itself is already ultra-simple and it reads very well. What
value would we get from moving this to a helper method? It’s currently
easy to open the view and know that “Foo bar baz” will only be
rendered for an admin. Does moving this to
“display_foo_bar_baz_for_admin” really give us anything?

There’s a cost to creating a helper method for every one-off
condition. There’s also a cost for turning every snippet of markup
that shows up in a simple view condition into a partial. The cost is
in the disconnect and separation that comes from doing separating
these pieces that go together as well as the organization nightmare of
a plethora of helper methods and partials.

I like extracting helpers when it provides more value than burden.
When it doesn’t I will leave the small, ultra-simple logic in the
view,

Zach


rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users


Zach D.
http://www.continuousthinking.com (personal)
http://www.mutuallyhuman.com (hire me)
http://ideafoundry.info/behavior-driven-development (first rate BDD
training)
@zachdennis (twitter)

On Sun, Jun 7, 2009 at 4:24 AM, Charlie B.[email protected]
wrote:

On Jun 6, 2009, at 10:02 PM, Zach D. [email protected] wrote:

end

That’s how I handle that. If for no other reason but easier testing.

But that refactoring is less intention revealing since it hides the
fact than only admins will see that message.

If I couldn’t come up with a better name for foo_message which
revealed that, I’d probably prefer leaving the if test in the view.

Resolving the tensions between things like “dumb views” and “intention
revealing names” is why they pay us the big bucks!


Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

Sent from my iPhone

On Jun 7, 2009, at 8:54 AM, Rick DeNatale [email protected]
wrote:

<% if current_user.admin? %>
fact than only admins will see that message.

If I couldn’t come up with a better name for foo_message which
revealed that, I’d probably prefer leaving the if test in the view.

Resolving the tensions between things like “dumb views” and “intention
revealing names” is why they pay us the big bucks!

Agreed. Should be def foo_message_if_admin
And yes I know im being extreme with my view on conditional logic in
views. I’ve found it very helpful to think this way about my views.
Just by refactoring old code this way I’ve found and fixed bugs that
hadn’t even surfaced yet.

Yes! We do use render in helpers to a great extent and are bitten by the
“missing template in view path” error too. Any workarounds? Can the view
path be easily fixed for helper specs in Rspec 1.3?
Does Rspec 2 already support this?

Cheers, Alex

David C. wrote:

On Thu, Jun 4, 2009 at 8:41 PM, Charlie B.
[email protected] wrote:

I have a helper method that does a “render :partial”.� The method works fine
within the app (Rails 2.3.2).� In rspec (1.2.6) I get an error (“Missing
template /comments/_comment.erb in view path”
It seems that rspec when running helper tests doesn’t set the view_path so
rails doens’t know where to look for the templates.� Is there a way to run
helper specs with the same setup as controller specs?

There is no support for rendering in helper specs as of yet. Please
file a feature request if you think there should be. I have,
personally, never rendered from a helper. Anybody else?

On Tue, Aug 24, 2010 at 5:19 AM, Alex Pressberg
[email protected]wrote:

Yes! We do use render in helpers to a great extent and are bitten by the
“missing template in view path” error too. Any workarounds? Can the view
path be easily fixed for helper specs in Rspec 1.3?
Does Rspec 2 already support this?

If you’re doing it, it’s obviously possible, but it feels wrong to me. I
think of a helper as a small piece of code that helps me decide what to
render, gives me a CSS class name based on some condition, gives me a
small
chunk of markup whose generation would clutter up the view, etc. Have
you
considered that, though the framework allows you to do this, it may not
be a
good approach? In other words, instead of looking for a workaround,
would it
make more sense (at least in the long term) to change your application?

I’m sorry I can’t offer actual help w/ current tools. I’m curious if
Rails 3
still supports this or if RSpec 2 will support it.

Regards,
Craig