[rspec-users] Specing Rails Views

Graeme Nelson graeme.nelson at gmail.com
Sun Feb 11 16:07:19 EST 2007


Hello -

I'm currently trying to write some specs for my rails views.  My  
views depend upon
the restful authentication plugin method logged_in?  Like so,


	<% if logged_in? %>
	<ul id="product-admin-nav">
		<dd><%= link_to "create a new product", new_product_url %></dd>
	</ul>
	<% end %>


However, when I have the following in my specification:

	render "products/show"

I get an error 'logged_in?' isn't defined for <#Class....>

My rails app gets 'logged_in?' via 'include AuthenticatedSystem' in  
my application controller.
I understand that views are tested in isolation from their controller  
counterpart in rpsec, so 'include AuthenticatedSystem'
isn't called and thus 'logged_in?' isn't available for the test/spec.

I found a couple of work arounds.

1)  add a method logged_in? into the application_helper.rb or  
<controller>_helper.rb (where <controller> is the controller  
associated with the current view being tested.)
2)  add a method in my setup to mock the :logged_in? call in the  
template, like so:


	@controller.template.stub!(:logged_in?).and_return(true)


I am sure there are other ways to make 'logged_in?' available to the  
template being tested. I am currently leaning towards #2 since it  
doesn't touch any of my application code.  Anyone else have some ideas?

Thanks in advanced.
graeme nelson




More information about the rspec-users mailing list