[rspec-devel] [ rspec-Bugs-6541 ] catch-22 using named routes

noreply at rubyforge.org noreply at rubyforge.org
Tue Nov 7 16:37:49 EST 2006


Bugs item #6541, was opened at 2006-11-07 14:39
You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3149&aid=6541&group_id=797

Category: rails plugin
Group: None
Status: Open
Resolution: None
Priority: 3
Submitted By: Nobody (None)
Assigned to: Nobody (None)
Summary: catch-22 using named routes

Initial Comment:
The recommended way specify controller redirects is to use

  controller.should_redirect_to 'path or hash'

*before* calling the action.

However, you cannot use named routes (XXX_url) until the action has been called (it gives 'rewrite' undefined for nil when it tries to call an instance variable in the controller).  

Can the mock be improved sufficiently to allow named routes to work in advance of the action? 

One workaround is to instantiate an ActionController::Integration::Session and delegate the named routes to that.

----------------------------------------------------------------------

Comment By: Jerry West (jjw)
Date: 2006-11-07 21:37

Message:
PS: 

def redirect
  controller.url_for(response.redirected_to)
end 

# that's because named routes return the path. While should_redirect_to takes paths, response.redirected_to is a Hash.  Yes, I know, I know, I've just found out about should_have_redirected_to or whatever it is.  I'm still coming to grips with the new equality rules and latest functionality!


----------------------------------------------------------------------

Comment By: Jerry West (jjw)
Date: 2006-11-07 21:25

Message:
Sorry, failing spec as requested...

----------------------------------------------------------
require File.dirname(__FILE__) + '/../spec_helper'

# just so we have something to test
class TestController < ApplicationController
  def index
    render :text => 'testing'   # don't look for view
  end
end # probably redundant with 0.7?

context "All controllers (via ApplicationController)" do  
  controller_name :test
  
  specify 'should redirect to login if protected page sought and not logged in' do
    controller.should_redirect_to login_url
    get :index
    controller.should_not_be_logged_in
    redirect.should == login_url
  end
end

----------------------------------------------------------

spec -fs spec/controllers/application_controller_spec.rb
/home/jjw/work/northants/config/environment.rb:31: warning: already initialized constant RAILS_CONNECTION_ADAPTERS
Creating sqlite in memory database...

All controllers (via ApplicationController)
- should redirect to login if protected page sought and not logged in (FAILED - 1)

1)
NoMethodError in 'All controllers (via ApplicationController) should redirect to login if protected page sought and not logged in'
You have a nil object when you didn't expect it!
The error occured while evaluating nil.rewrite
generated/routing/named_routes/login.rb:2:in `login_url'
./spec/controllers/application_controller_spec.rb:17:in `should redirect to login if protected page sought and not logged in'

Finished in 0.004108 seconds

1 specification, 1 failure

---------------------------------------------------------

commenting out the offending 'should_redirect_to' shows that the code itself is fine and login_url valid after the call to get:-

All controllers (via ApplicationController)
- should redirect to login if protected page sought and not logged in

Finished in 0.006735 seconds

1 specification, 0 failures

----------------------------------------------------------

Examining the code for ActionController::Base shows that the nil value is an unset instance variable (@url).  There is a method 'initialize_current_url' (line 927) which passes @request and @params to UrlRewriter.new, if that's any help.  

----------------------------------------------------------------------

Comment By: Aslak Hellesøy (aslak_hellesoy)
Date: 2006-11-07 17:46

Message:
Could you please attach a failing spec as explained here?
http://rspec.rubyforge.org/contribute.html

----------------------------------------------------------------------

You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3149&aid=6541&group_id=797


More information about the rspec-devel mailing list