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
On 2/11/07, Graeme Nelson <graeme.nelson at gmail.com> wrote:> 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) >This looks like a fine way to do it, to me. Treating logged_in? as just another attribute to be passed to the view (albeit in a slightly specialized way, as it is a method called in the view) seems like the right thing. This would make it easy to spec views in both the logged in and logged out cases. I still haven''t gotten into specing views - as long as they don''t throw nasty errors, and they are valid xml, I let my designers handle the work. But once we stablize the look and feel, I''ll probably be looking to add some regression type verification on the views. -- Chris Anderson http://jchris.mfdz.com