Hi All,
I’m writing tests for my new rails app and I’ve run into a bit of a
brick
wall. I’m trying to write a test for my ApplicationController which has
a
simple authorize method. And when I try and run the test I’m hitting a
nil
object exception. Here’s what my ApplicationController looks like:
class ApplicationController < ActionController::Base
before_filter :authorize, :except => :login
helper :all # include all helpers, all the time
protect_from_forgery # See ActionController::RequestForgeryProtection
for
details
Scrub sensitive parameters from your log
filter_parameter_logging :password
public
def authorize
unless User.find_by_id(session[:user_id])
session[:original_uri] = request.request_uri
flash[:notice] = “Please log in”
redirect_to :controller => ‘access_control’, :action => ‘login’
end
end
end
I’ve created a test for the Application Controller and tried to mock out
the
controller as much as possible but I don’t think I’ve covered
everything.
require ‘test_helper’
class ApplicationControllerTest < ActionController::TestCase
def setup
@controller = ApplicationController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
@controller.session = ActionController::TestSession.new
end
require ‘test_helper’
class ApplicationControllerTest < ActionController::TestCase
def setup
@controller = ApplicationController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
@controller.request = @request
@controller.response = @response
@controller.session = ActionController::TestSession.new
end
def test_authorize_works_when_user_logged_in
#setup
logged_in
@request.request_uri = “http://test.host/”
#exercise
ApplicationController.new.authorize
#assertions
assert_equals @request_uri = “http://test.host/”
end
end
When I run my tests this is what I get :
- Error:
test_authorize_works_when_user_logged_in(ApplicationControllerTest):
NoMethodError: You have a nil object when you didn’t expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]
app/controllers/application_controller.rb:14:inauthorize' functional/application_controller_test.rb:21:in
test_authorize_works_when_user_logged_in’
Its failing on the line where I try to look up the user from the session
object.
Is there a better way to mock out the ApplicationController? Why
doesn’t
rails generate a test in the first place for the application controller
am I
barking up the wrong tree? I have execised this code from another
controller test but it doesn’t feel right to have to use another
controller
to test this, and in the future I can see it being awkward when I want
to
test locale or cookie functionality.
I’ve had a good search for this and found a lot of questions relating to
this but no good answers which leads me to believe that I’m missing an
obvious convention of rails around not testing the application
controller.
Any help or advise would be appreciated.
–
View this message in context:
http://old.nabble.com/Finding-it-difficult-to-test-a-before_filter-on-my-ApplicationController-tp28253771p28253771.html
Sent from the RubyOnRails Users mailing list archive at Nabble.com.