[rspec-users] Testing my rails authentication controller

powertoaster powertoaster at hotmail.com
Fri Jun 19 14:30:16 EDT 2009


David thank you, that worked great. I am doing this and am very happy with
the result.

      Employee.stub(:authenticate).with(params[:username],
params[:password]).and_return(nil)
      post :login, params
      session[:employee_id].should be_nil
      response.should redirect_to(:controller => "access", :action =>
"login")

The question I have now is how do I test the other controllers. Since all of
my other pages are going to require authentication.

How do I mock the login functionality in a way that allows me to test my
protected pages as if I were logged in without using an actual post to the
login controller, and still get the :employee_id in the session?

I tried adding this in a before each do

    @params = {:username => 'MyString', :password => 'MyString'}
    Employee.stub(:authenticate).with(params[:username],
params[:password]).and_return(mock_model(Employee, :id => 1))
    session[:employee_id] = 1

Which successfully sets the session variable, but my test fails.

describe "GET index" do
    it "assigns all details as @details" do
      Detail.stub!(:find).with(:all).and_return([mock_detail])
      get :index
      session[:employee_id].should == 1                          <+++++++++
This passes
      assigns[:details].should == [mock_detail]                 <+++++++++
This fails
    end
  end

I get 

expected: [#<Detail:0x23246b2 @name="Detail_1001">],
     got: nil (using ==)





-- 
View this message in context: http://www.nabble.com/Testing-my-rails-authentication-controller-tp23895779p24116711.html
Sent from the rspec-users mailing list archive at Nabble.com.



More information about the rspec-users mailing list