Rails: View specs and implicit parameters in link_to()


#1

Hi all,

I’ve been cleaning up our routing file, and removed the default
map.connect “:controller/:action” route.

It’s thrown up a bunch of sloppy mistakes, which is great, but I also
think I’ve found a problem with the view specs.

We have a generic navbar partial which is rendered in the index page
of several different controllers. The navbar contains some call to
link_to of the form:

<%= link_to “By Concert”, :sort => ‘by_concert’ %>

Since I removed the default (catch-all) route, this fails in the view
specs:

No route matches {:action => “index”, :sort => “by_concert”

At runtime, rails seems to figure out the controller as well, and we
get a full URL built fine.

I tried adding this to my specs

params[:controller] = “images”

but nothing changed. I’ve worked around it by also doing this in the
view:

<%= link_to “By Concert”, :controller => params[:controller], :sort
=> ‘by_concert’ %>

This seems kind of dirty… and it’s also weird that I can’t seem to
be able to fake the context that the views render in at runtime.

What do other people do with this sort of problem? Am I missing
something obvious?

cheers,
Matt


#2

I’m in the same boat - trying to test a helper that uses the
implicit :controller in a link_to, and I get an exception from rails:
ActionController::RoutingError in ‘ApplicationHelper breadcrumbs
should return a link to elems section’
Need controller and action!
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/
action_controller/routing/route_set.rb:350:in `generate’
:
:
:

is there a way to tell the rspec-rails helper examples that the
request that called the specified helper was to a certain controller?
anyone have a clean way of doing this?


#3

On Thu, Oct 16, 2008 at 5:31 AM, Matt W. removed_email_address@domain.invalid wrote:

the form:
I tried adding this to my specs

What do other people do with this sort of problem? Am I missing something
obvious?

This looks to be solved in in master. See
http://rspec.lighthouseapp.com/projects/5645/tickets/488


Zach D.
http://www.continuousthinking.com
http://www.mutuallyhuman.com


#4

On 21 Nov 2008, at 14:40, Higgaion wrote:

is there a way to tell the rspec-rails helper examples that the
request that called the specified helper was to a certain controller?
anyone have a clean way of doing this?

Yeah looking back I never did get any help with this one, did I?

I personally sorted the situation out by cleaning up the code (it was
a bit legacy) such that I could use named routes instead. I never
really got to the bottom of how the implicit :controller stuff worked,
especially in the context of an RSpec ExampleGroup, and it seemed
cleaner to be using a named route. Probably easier said than done
though.

There’s also a trick in this thread that might just help you:

http://www.ruby-forum.com/topic/169004

HTH,
Matt