jeudi 30 juin 2016

Ruby returns nil class in testing, working fine in dev

I'm trying to implement a login system using rails rather than an external gem and have been following the Michael Hartl tutorial most apparently pop their cherry with. So far the site itself is functioning fine, it's 2 tests to do with logging in I'm struggling with:

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" do
      get login_path
      post login_path, params: { session: { email:    @user.email,
                                      password: 'password' } }
      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)
    end

  end

My error messages are:

ERROR["test_login_with_invalid_information", UsersLoginTest, 2016-06-30 22:30:36 +0100]
test_login_with_invalid_information#UsersLoginTest (1467322236.17s)
  NoMethodError:         NoMethodError: undefined method `[]' for nil:NilClass
        app/controllers/sessions_controller.rb:7:in `create'
        test/integration/users_login_test.rb:12:in `block in <class:UsersLoginTest>'
    app/controllers/sessions_controller.rb:7:in `create'
    test/integration/users_login_test.rb:12:in `block in <class:UsersLoginTest>'

  ERROR["test_login_with_valid_information", UsersLoginTest, 2016-06-30 22:30:36 +0100]
   test_login_with_valid_information#UsersLoginTest (1467322236.18s)
  NoMethodError:         NoMethodError: undefined method `[]' for nil:NilClass
        app/controllers/sessions_controller.rb:7:in `create'
        test/integration/users_login_test.rb:21:in `block in       <class:UsersLoginTest>'
    app/controllers/sessions_controller.rb:7:in `create'
    test/integration/users_login_test.rb:21:in `block in       <class:UsersLoginTest>'

The error codes point to the following controller:

  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
        redirect_to user
      else
        flash.now[:danger] = 'Invalid email/password combination'
        render 'new'
      end
    end

    def destroy
    end
  end

I assumed at the start this was due to the sessions helper methods not being available, however the site itself runs fine in development mode and logging in is possible. Am I missing something from my sessions helper or the test file itself? My sessions helper:

module SessionsHelper

  # Logs in the given user.
  def log_in(user)
    session[:user_id] = user.id
  end

  # Returns the current logged-in user (if any).
  def current_user
    @current_user ||= User.find_by(id: session[:user_id])
  end

  # Returns true if the user is logged in, false otherwise.
  def logged_in?
    !current_user.nil?
  end
end

And finally my application controller:

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  include SessionsHelper
end

Apologies for the wall of text and thanks in advance

Aucun commentaire:

Enregistrer un commentaire