Reputation: 391
Ok so I have made a login action but I'm a bit confused on how I would setup a functional or unit test for this. Would it be something like send a username and password from on of my fixtures and check if the session[:user_id] is nil?
Thanks in advance :)
#POST /sessions
def create
user = User.authenticate(params[:email],params[:password])
if user
session[:user_id] = user.id
redirect_to root_path, :notice => "Logged in successfully!"
else
flash.now.alert = "Invalid username/password"
render "new"
end
end
Upvotes: 1
Views: 412
Reputation: 2059
Logically, there are several tests that you can make:
Assuming that you are using the standard rails test framework, this would be placed under the functional tests for your session controller. You'd test for redirects and renders by doing things like:
assert_template "new"
and
assert_redirected_to root_path
More info here (especially about the hashes that are available after you've done a post):
http://guides.rubyonrails.org/testing.html#functional-tests-for-your-controllers
Upvotes: 2