[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)
   Group.stub!(:search_with_paginate).and_return(@group)

    

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

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

    self.stub!(:login_required).and_return(:false)

    self.stub!(:current_user).and_return(@current_user)

  end

  

  def do_get

    get :index  

  end

  

  it "should be successful" do

    assigns[:page] = 1

    assigns[:search] = ""

    do_get

    puts response.headers

    response.should be_success

  end

end

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