2016-07-01 58 views
0

我試圖實現一個使用rails而不是外部gem的登錄系統,並且一直遵循Michael Hartl教程最明顯地彈出它們的櫻桃。到目前爲止,本身是精功能的網站,它2個測試做記錄在我掙扎:Ruby在測試中返回nil類,在dev中工作正常

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 

我的錯誤信息是:

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>' 

錯誤碼指向以下控制器:

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 

我在一開始以爲這是由於不是可用的會議輔助方法,但該網站本身運行在開發模式精細和登錄是可能的。我是否缺少會議幫手或測試文件本身的內容?我的會議助手:

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 

最後我的應用程序控制器:

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 

道歉文字和感謝的牆壁提前

+0

當它說未定義的方法...對於nilclass,這意味着什麼是你不期望的東西。最簡單的調試方法是使用類似於byebug的東西,並找出究竟是什麼值無 –

回答

0

我認爲,如果你改變了這個

post login_path, params: { session: { email: "", password: "" } } 

納入此

post login_path, session: { email: "", password: "" } 

在您的第一次測試中,它會通過。如果確實如此,請嘗試與第二種策略相同的策略。如果失敗了,我建議你在開始時在控制器中創建一個方法raise params.inspect,運行測試來檢查參數,然後從你學到的東西中確定解決方案。

+0

非常感謝 - 這完美的工作! – Mark

相關問題