[rspec-users] Need help using URL helpers in controller tests

Christopher Bailey chris at cobaltedge.com
Fri Oct 10 10:37:08 EDT 2008

I've moved on from this with a bit different approach.  But, I also found
that the "get" (or whichever http method used) call expects the first
parameter to be an action, so passing in a string that is a path or URL
doesn't work for that.

On Fri, Oct 10, 2008 at 1:04 AM, Matt Wynne <matt at mattwynne.net> wrote:

> On 9 Oct 2008, at 20:24, Christopher Bailey wrote:
>  I have a controller test, where I want to do a GET on a page.  Our URL's
>> are complex, and need to be correct (duh, but we allow some slop, but that
>> causes a redirect which I want to avoid).  Anyway, in my controller test I
>> do a get to the URL that is produced by a helper method which calls a named
>> route URL helper.  This is not working, and I'm wondering why/what I'm
>> messing up.  Here's the line in the spec code that starts things off
>> (@widget is a real ActiveRecord object):
>>      get @controller.widget_path_for_seo(@widget)
>> The widget_path_for_seo method is defined in our ApplicationController.
>>  It digs some info out of the widget instance that then get passed to the
>> named route URL helper method, so it looks like this:
>>  def widget_path_for_seo(widget)
>>    location = seo_name_for_url widget.location.name
>>    building = seo_name_for_url(building_name_for_seo_url(widget.building),
>> true) + "-widgets"
>>    widget_id = seo_name_for_url(widget.name) + "-" + widget.id.to_s
>>    seo_widget_path(:location => location, : building => building, :id =>
>> widget_id)
>>  end
>> It's the "seo_widget_path" named route helper method that is failing.
>>  I've checked that all values going into it are valid, but the error I get
>> is in ActionController::Base.url_for, where it's looking at the options hash
>> of values that are passed in, and doing this:
>>            @url.rewrite(rewrite_options(options))
>> It compalins that @url is nil.  I'm wondering if this is due to being run
>> under the test environment or what I'm missing/not realizing, etc.  What's
>> strange is that some of the restful resource route url helpers work fine.
>>  Can anyone clue me in?
>> --
>> Christopher Bailey
>> Cobalt Edge LLC
>> http://cobaltedge.com
> Quick thought - it doesn't look like you have 'spun' up the routes when you
> test this method. If you haven't already made a get/post request through the
> rails integration session infrastructure the named routes don't get loaded,
> and that can be quite confusing. I'm not sure if that could be the cause of
> your issue but I thought it was worth mentioning.
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

Christopher Bailey
Cobalt Edge LLC
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20081010/cc4624b1/attachment.html>

More information about the rspec-users mailing list