1

I am testing this controller:

class SessionsController < ApplicationController
  def new
  end

  def create
    @current_user = User.find_by_login_and_password(
      params[:login], params[:password]
    )

    if @current_user
      session[:user_id] = @current_user.id
      if session[:return_to]
        redirect_to session[:return_to]
        session[:return_to] = nil
      else
        redirect_to stories_path
      end
    else
      render :action => 'new'
    end
  end

  def destroy
    session[:user_id] = @current_user = nil
  end

end

with this fixture (users.yml):

patrick:
  login: patrick
  password: sekrit
  name: Patrick Lenz
  email: [email protected]

john:
  login: john
  password: gh752px
  name: John Doe
  email: [email protected]

and this functional test:

require 'test_helper'

class SessionsControllerTest < ActionController::TestCase
  def test_should_show_login_form
    get :new
    assert_response :success
    assert_template 'new'
    assert_select 'form p', 4
  end

  def test_should_perform_user_login
    post :create, :login =>'patrick', :password => 'sekrit'
    assert_redirected_to stories_path
    assert_equal users(:patrick).id, session[:user_id]
    assert_equal users(:patrick), assigns(:current_user)
  end
end

However, I get this failure:

test_should_perform_user_login(SessionsControllerTest) [/home/ygamretuta/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-2.3.9/lib/action_controller/test_case.rb:119]:
Expected response to be a <:redirect>, but was <200>.
Expected block to return true value

the login logic is working but my tests fail, my route to the stories_path:

story_votes GET    /stories/:story_id/votes(.:format)          {:controller=>"votes", :action=>"index"}
                POST   /stories/:story_id/votes(.:format)          {:controller=>"votes", :action=>"create"}
 new_story_vote GET    /stories/:story_id/votes/new(.:format)      {:controller=>"votes", :action=>"new"}
edit_story_vote GET    /stories/:story_id/votes/:id/edit(.:format) {:controller=>"votes", :action=>"edit"}
     story_vote GET    /stories/:story_id/votes/:id(.:format)      {:controller=>"votes", :action=>"show"}
                PUT    /stories/:story_id/votes/:id(.:format)      {:controller=>"votes", :action=>"update"}
                DELETE /stories/:story_id/votes/:id(.:format)      {:controller=>"votes", :action=>"destroy"}
        stories GET    /stories(.:format)                          {:controller=>"stories", :action=>"index"}
                POST   /stories(.:format)                          {:controller=>"stories", :action=>"create"}
      new_story GET    /stories/new(.:format)                      {:controller=>"stories", :action=>"new"}
     edit_story GET    /stories/:id/edit(.:format)                 {:controller=>"stories", :action=>"edit"}
          story GET    /stories/:id(.:format)                      {:controller=>"stories", :action=>"show"}
                PUT    /stories/:id(.:format)                      {:controller=>"stories", :action=>"update"}

What could I be missing?

EDIT: This is the structure of my users table (it's using a plain text password only for the book examples)

CREATE TABLE "users" (
 "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 
 "login" varchar(255), 
 "password" varchar(255), 
 "name" varchar(255), 
 "email" varchar(255), 
 "created_at" datetime, 
 "updated_at" datetime
)

1 Answer 1

3

Ygam, I'm 99% sure that the problem is that the password doesn't match. Are you using salted / encrypted passwords? if that's the case, check that your fixture has plain text passwords.

I'm quite confident that this is the case, because you're doing a post and getting a 200, and the only path in create that returns a 200, is when the user / password pair validation fails.

If you're using salted passwords, you might need to do something like this in your fixture

<% SALT = "NaCl" unless defined?(SALT) %>
one:
  name: dave
  hashed_password: <%= User.encrypt_password('secret', SALT) %>
  salt: <%= SALT %>

two:
  name: MyString
  hashed_password: MyString
  salt: MyString
Sign up to request clarification or add additional context in comments.

5 Comments

the book I'm walking through had plain text passwords only for example, that's how it is in my sqlite file. Let me try your solution, I'll get back to u later
did not work. I am not using encrypted passwords because that's how the book went. is there any way that I can make this work with plain text passwords?
Ygam, can you upload the application somewhere? Also, which book are you are you using? as you haven't mentioned the title :)
Simply Rails 2 by Sitepoint. Having some troubles with it because the book was written back at 2008, with version 2.0 of Rails. Sorry, I can't upload it anywhere.
got it to work. I was missing a fixtures :all in my test_helper.rb. I'll accept this answer because it would be the most suitable one if my case wasn't as stupid as missing one line in my test_helper T_T

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.