[rspec-users] Testing my rails authentication controller
dchelimsky at gmail.com
Fri Jun 5 23:38:56 EDT 2009
On Fri, Jun 5, 2009 at 3:10 PM, Wayne Andersen <waynea at clima-tech.com> wrote:
> I have a simple controller:
> class AccessController < ApplicationController
> def login
> if request.post?
> employee = Employee.authenticate(params[:name], params[:password])
> if employee
> session[:employee_id] = employee.id
> redirect_to(:controller => "timesheets", :action => "index")
> flash.now[:notice] = "Invalid username/password combination"
> I have tried a number of ways to create a spec to test the login function,
> but am not having any luck.
> My current code looks something like this, but I don’t really understand
> what is going on here.
This line overrides the login action with a message expectation, so
the login action in the controller is never executed. What you want is
to stub the decision points and then set expectations about what
happens. For example:
it "assigns the employee id to the session if the employee is found" do
Employee.stub(:authenticate).and_return(mock_model(Employee, :id => 37))
post :login, params
session[:employee_id].should == 37
Let me know if that is not self-explanatory and I'll explain.
> post :login, params
> session[:employee_id].should_not be_nil
> I am setting a simple expectation in the first line and then doing a post
> with params. I then check to see if the variable is in the session.
> What is the best way to make something like this work..
> Wayne L. Andersen
In the firs tlin
More information about the rspec-users