mardi 11 avril 2017

Expected "G-WWU6wMDhCr3VZ3YnHglw" to be empty -> RoR Tutorial Chapter 9

currently working on chapter 9 (Listing 9.26) of Hartls RoR Tutorial I experiece the following error while testing:

Failure:
UsersLoginTest#test_login_without_remembering [c:/Sites/workspace/sample_app/test/integration/users_login_test.rb:53]:
Expected "G-WWU6wMDhCr3VZ3YnHglw" to be empty.

/users_login_test.rb

require 'test_helper'

class UsersLoginTest < ActionDispatch::IntegrationTest

  def setup
    @user = users(:michael)
  end



    test "login with invalid information" do
      get login_path
      assert_template 'sessions/new'
      post login_path, params: { session: { email: "", password: "" } }
      assert_template 'sessions/new'
      assert_not flash.empty?
      get root_path
      assert flash.empty?
    end

    test "login with valid information followed by logout" do
      get login_path
      post login_path, params: { session: { email:    @user.email,
                                            password: 'password' } }
      assert is_logged_in?
      assert_redirected_to @user
      follow_redirect!
      assert_template 'users/show'
      assert_select "a[href=?]", login_path, count: 0
      assert_select "a[href=?]", logout_path
      assert_select "a[href=?]", user_path(@user)
      delete logout_path
      assert_not is_logged_in?
      assert_redirected_to root_url
      # Simulate a user clicking logout in a second window.
      delete logout_path
      follow_redirect!
      assert_select "a[href=?]", login_path
      assert_select "a[href=?]", logout_path,      count: 0
      assert_select "a[href=?]", user_path(@user), count: 0
    end

    test "login with remembering" do
      log_in_as(@user, remember_me: '1')
      assert_not_empty cookies['remember_token']
    end

    test "login without remembering" do
      # Log in to set the cookie.
      log_in_as(@user, remember_me: '1')
      # Log in again and verify that the cookie is deleted.
      log_in_as(@user, remember_me: '0')
      assert_empty cookies['remember_token']
    end
  end

/test_helper.rb

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'

class ActiveSupport::TestCase
  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
  fixtures :all

  #returns true if a test user is logged in
  def is_logged_in?
    !session[:user_id].nil?
  end

  # Log in as a particular user.
  def log_in_as(user)
    session[:user_id] = user.id
  end
end

class ActionDispatch::IntegrationTest

  # Log in as a particular user.
  def log_in_as(user, password: 'password', remember_me: '1')
    post login_path, params: { session: { email: user.email,
                                          password: password,
                                          remember_me: remember_me } }
  end
end

/sessions_controller.rb

class SessionsController < ApplicationController

  def new
  end

  def create
    user = User.find_by(email: params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password])
      log_in user
      params[:session] [:remember_me] == '1' ? remember(user) : forget(user)
      remember user
      redirect_to user
    else
      flash.now[:danger] = 'Invalid email/password combination'
      render 'new'
    end
  end

  def destroy
    log_out if logged_in?
    redirect_to root_url
  end
end

I'm confused, let me know if you need any other files.

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire