[rspec-users] How do I mock out the before :login_required method?

Wes Shaddix wshaddix at gmail.com
Tue Feb 12 22:12:50 EST 2008

I have a GroupController class that inherits from a SecuredController 
which have a before filter (before_filter :login_required). This is 
using the restul authentication system. I want to mock out the 
login_required method so that my GroupController actions don't get 
redirected to /sessions/new but I cant figure it out. Here is what I 
have so far that doesn't work. Any help would be most appreciated.

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

describe GroupsController do

  before(:each) do

   # mock and stub the Group model methods
   @group = mock_model(Group)


    # since this is a secured controller, we have to mock the security system too

    @current_user = mock_model(User, :id => 1)





  def do_get

    get :index  



  it "should be successful" do

    assigns[:page] = 1

    assigns[:search] = ""


    puts response.headers

    response.should be_success



The error I get is 
NoMethodError in 'GroupsController should be successful'
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.[]=

More information about the rspec-users mailing list