[rspec-users] Rails: possible routing discrepancy

Francis Hwang sera at fhwang.net
Sun Dec 23 12:23:52 EST 2007

Hey all, I'm seeing a strange behavior in my spec that I can't  
account for. I've got a route that looks like this:

map.writing \
   :controller => 'abstracts', :action => 'index',
   :index => { :select => 'all' }

and I have template code that looks like this:

<br /><%= link_to_unless_current 'Writing', writing_path %>

and I have a spec which calls this page, and checks to see that  
'Writing' is not a link:

describe "'/writing/'" do
   controller_name :abstracts

   it "should not link 'Writing'" do
     get 'index', :index => { :select => 'new' }
     response.body.should_not have_tag( 'a', 'Writing' )

... and it fails.

Normally, I'd assume I just messed up writing my routes, which is an  
easy enough mistake for me to make. But when I actually open up my  
browser and go to /writing, I see that the text is not being linked.  
And the params hash that's visible in my application log shows the  
same params I think I'm generating in my spec:

Parameters: {"action"=>"index", "controller"=>"abstracts", "index"=> 

but there seems to be a discrepancy nonetheless.

Then I started poking around in action_view/helpers/url_helper.rb, in  
current_page?, which is used by link_to_unless_current. Here's that  

def current_page?(options)
   url_string = CGI.escapeHTML(url_for(options))
   request = @controller.request
   if url_string =~ /^\w+:\/\//
     url_string == "#{request.protocol}#{request.host_with_port}# 
     url_string == request.request_uri

So here's the rub: When I'm running current_page? with script/server,  
I get "/writing" for url_string and "/writing" for  
@controller.request.request_uri -- meaning current_page? works  
correctly, and link_to_unless_current works correctly.

But, when I'm running current_page? from my spec, I get "/writing"  
for url_string and "/abstracts?index%5Bselect%5D=new" for  
@controller.request.request_uri -- so current_page? works incorrectly  
and so does link_to_unless_current.

Looks like that @controller.request is an RSpec request of some kind.  
Is it possible the RSpec request isn't handling route generation  
correctly? Or should I be writing my spec differently?


Francis Hwang

More information about the rspec-users mailing list