[rspec-users] Specs for ApplicationController, where to put them?

Anthony Carlos anthony at digitalphenom.com
Fri Jun 1 18:20:32 EDT 2007

On May 29, 2007, at 7:52 PM, David Chelimsky wrote:

>> My problem now, is that I get a nil object error. Specifically, in
>> application.rb, I'm trying to call request.parameters and request is
>> nil. It seems that request is not part of the context anymore since I
>> moved the behavior out to a separate file. How do I regain access to
>> request, response, assigns, flash, and session?
> The fact that the shared examples are in another file should have no
> bearing on whether they have runtime access to whatever is in the
> behaviours that include them. This all sounds odd to me. Can you post
> a backtrace please?


Here's the output from rake spec:autotest:

- should flash notice when session[:current_user_id] does not exist  
(ERROR - 1)
- should return true when session[:current_user_id] exists
- should respond to current_user
- should respond to authenticate

NoMethodError in 'QuestionnairesController should flash notice when  
session[:current_user_id] does not exist'
You have a nil object when you didn't expect it!
The error occurred while evaluating nil.parameters
11:in `authenticate'

Here's application_controller_spec.rb:
require File.dirname(__FILE__) + '/../spec_helper'

unless Spec::DSL::Behaviour.find_shared_behaviour("All controllers")
   describe "All controllers", :shared => true do
     it "should respond to authenticate" do
       controller.should respond_to(:authenticate)
     it "should respond to current_user" do
       controller.should respond_to(:current_user)
     it "should return true when session[:current_user_id] exists" do
       session[:current_user_id] = 12345
       controller.authenticate.should == true
     it "should flash notice when session[:current_user_id] does not  
exist" do
       session[:current_user_id] = nil
       controller.authenticate # <=====  This is line 24 where the  
error occurs!
       flash[:notice].should == 'Please login.'

Here's application.rb:
class ApplicationController < ActionController::Base
   # Pick a unique cookie name to distinguish our session data from  
   session :session_key => '_unique_session_id'

   def authenticate
     unless session[:current_user_id]
       flash[:notice] = 'Please login.'
       session[:jumpto] = request.parameters # <===== This is line 11  
where nil.parameters is called!
       redirect_to(:controller => 'ums', :action => 'login')

   def current_user
     @current_user = User.find(session[:current_user_id])

Finally, here's questionnaires_controller_spec.rb:
require File.dirname(__FILE__) + '/../spec_helper'
require File.dirname(__FILE__) + '/application_controller_spec'  
unless Spec::DSL::Behaviour.find_shared_behaviour("All controllers")

describe QuestionnairesController do
   it_should_behave_like "All controllers"

So as far as I can tell, my QuestionnairesController should behave  
like "All controllers" which puts us at  
application_controller_spec.rb. The first 3 examples pass, but the  
fourth, which calls ApplicationController.authenticate, gives me the  
nil object. But, isn't request an object that is always available to  

Wait a second...

There's no get or post command here! Maybe thats why request is nil?  
I think so. Never mind! Sorry about the false alarm. I don't want to  
be the boy who cried wolf.

On the other hand, I think I avoided using get or post because this  
is an abstract action that gets called by other concrete classes. So  
how does one test logic in an abstract class? Smells like a job for a  
mock, of course?!? I'll look over the list archive-- I think other  
people have asked this question before.

Thanks for helping out the slow learners,


More information about the rspec-users mailing list