Rails教程第3版。 NoMethodError:未定义的方法`remember_token'为零:NilClass

时间:2016-01-19 10:29:01

标签: ruby-on-rails ruby railstutorial.org

我在9章Rails教程中测试了问题,但是从第8章开始测试的问题。有人可以帮我解决吗?

问题在这里 test "login with remembering" do log_in_as(@user, remember_me: '1') assert_not_nil cookies['remember_token'] assert_equal cookies['remember_token'], assigns(:user).remember_token end

测试说明了

中的问题
NoMethodError: undefined method `remember_token' for nil:NilClass
            test/integration/users_login_test.rb:43:in `block in <class:UsersLoginTest>'
        test/integration/users_login_test.rb:43:in `block in <class:UsersLoginTest>'

我的完整user_login_test

    require 'test_helper'

class UsersLoginTest < ActionDispatch::IntegrationTest

  def setup
    @user = users(:michael)
  end

  test "login with invalid information" do #INVALID
    get login_path
    assert_template 'sessions/new'
    post login_path, 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, 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_nil cookies['remember_token']
    assert_equal cookies['remember_token'], assigns(:user).remember_token
  end

  test "login without remembering" do
    log_in_as(@user, remember_me: '0')
    assert_nil cookies['remember_token']
  end

end

我的用户控制器是

    class UsersController < ApplicationController
  before_action :logged_in_user, only: [:edit, :update]
  before_action :correct_user,   only: [:edit, :update]

  def show
    @user = User.find(params[:id])
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
      log_in @user
      flash[:success] = "Welcome to the Sample App!"
      redirect_to @user
    else
      render 'new'
    end
  end

  def edit
    @user = User.find(params[:id])
  end

  def update
    @user = User.find(params[:id])
    if @user.update_attributes(user_params)
      flash[:success] = "Profile updated"
      redirect_to @user
    else
      render 'edit'
    end
  end

   private

    def user_params
      params.require(:user).permit(:name, :email, :password,
                                   :password_confirmation)
    end

  # Before filters

   # Confirms a logged-in user.
    def logged_in_user
      unless logged_in?
        store_location
        flash[:danger] = "Please log in."
        redirect_to login_url
      end
    end

     # Confirms the correct user.
    def correct_user
      @user = User.find(params[:id])
      redirect_to(root_url) unless current_user?(@user)
    end

end

所有测试都是绿色的,直到我从Chapter 9.2.3添加一些代码然后我遇到了这个问题。我是Ruby的新手,对我来说解决这个问题有点困难。 最好的问候,对不起我的英语)

1 个答案:

答案 0 :(得分:1)

问题是assigns[:user]是零。 log_in_as是一个测试帮助程序,它不会调用您的控制器,因此分配始终为空。此外,它并没有真正测试你的控制器。您需要将其写为其余测试:

  test "login with remembering" do
    post login_path, session: { email: @user.email, password: 'password', remember_me: '1' }

    assert_not_nil cookies['remember_token']
    assert_equal cookies['remember_token'], assigns(:user).remember_token
  end