Hi,
On Sun, Jul 3, 2011 at 09:53, John Hinnegan <john.hinnegan at gmail.com>
wrote:> I''m testing methods in my ApplicationController using anonymous
controller
> class (not sure if that''s relevant).? I''ve somehow broken
something such
> that I do not have access to the routing variables that rails generates.
>
> So rather than having something like ''response.should redirect_to
> new_user_session_path'' I''m having to go with
''response.should redirect_to
> ''/users/sign_in''
>
> I''ve created a new project to check if I messed anything in
spec_helper or
> something else, and I''ve been unable to reproduce outside of my
current
> project.? I can access the route variables from other controller spec
> classes, but not from the application_controller_spec.
>
> Can anyone point me at where to look to start troubleshooting this?
>
> Rails 3.0.5
> $ gem list | grep rspec
> rspec (2.5.0)
> rspec-core (2.5.2)
> rspec-expectations (2.5.0)
> rspec-mocks (2.5.0)
> rspec-rails (2.5.0)
>
>
> Code snippets
> /app/controllers/application_controller.rb
> class ApplicationController < ActionController::Base
> ? protect_from_forgery
>
> ? def require_bar
> ??? if
> !user_signed_in?
> ????? redirect_to new_user_session_path
> ????? return
> ??? end
> ?? # check for bar here
> ? end
> end
>
> /spec/controllers/application_controller_spec.rb
> require ''spec_helper''
>
> describe ApplicationController do
> ? describe "#require_bar" do
> ??? controller do
> ????? before_filter :require_bar
> ????? def index
> ??????? render :text => "", :status => 200
> ????? end
> ??? end
>
> ??? context "when not signed in" do
> ????? before :each do
> ??????? controller.stub(:user_signed_in?).and_return(false)
> ????? end
> ????? it "should fail gracefully by redirecting to log in" do
> ??????? get :index
> ??????? # This works
> ??????? response.should redirect_to ''/users/sign_in''
> ??????? # this causes: undefined local variable or method
> `new_merchant_session_path'' for
>
#<RSpec::Core::ExampleGroup::Nested_1::Nested_2::Nested_1:0x000001034cd160>
> ??????? # response.should redirect_to new_user_session_path
> ????? end
> ??? end
> ? end
RSpec''s anonymous controller does not use your routes. If you look at
how the controller method is implemented[1], you''ll see the routes
being replaced with just the anonymous resources.
You can do something similar in a before block in your spec. For example:
describe ApplicationController do
? describe "#require_bar" do
??? controller do
????? before_filter :require_bar
????? def index
??????? render :text => "", :status => 200
????? end
??? end
before do
@routes.draw do
resources :anonymous # for all other specs to keep working
resources :user_sessions # this makes new_user_session_path available
end
end
??? context "when not signed in" do
????? before :each do
??????? controller.stub(:user_signed_in?).and_return(false)
????? end
????? it "should fail gracefully by redirecting to log in" do
??????? get :index
??????? response.should redirect_to new_user_session_path
????? end
??? end
? end
end
Hope that helps,
Mike
[1]
https://github.com/rspec/rspec-rails/blob/master/lib/rspec/rails/example/controller_example_group.rb#L127