Michael Hartl的教程第10.2.2章列出了10.26次测试失败

时间:2017-10-24 15:18:29

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

通过Hartl的教程并搜索了答案,但没有找到答案。 (可能存在答案,但是因为章节已经更新并在新版本中切换,如果它们存在,我无法找到它们。)

我在第10.2.2节。列出10.26进行轨道测试。

这是我测试的错误:

ERROR["test_should_redirect_edit_when_logged_in_as_wrong_user", UsersControllerTest, 1.5802158990409225]
 test_should_redirect_edit_when_logged_in_as_wrong_user#UsersControllerTest (1.58s)
BCrypt::Errors::InvalidHash:         BCrypt::Errors::InvalidHash: invalid hash
            app/controllers/sessions_controller.rb:9:in `create'
            test/test_helper.rb:32:in `log_in_as'
            test/controllers/users_controller_test.rb:29:in `block in <class:UsersControllerTest>'

ERROR["test_should_redirect_update_when_logged_in_as_wrong_user", UsersControllerTest, 1.606778411893174]
 test_should_redirect_update_when_logged_in_as_wrong_user#UsersControllerTest (1.61s)
BCrypt::Errors::InvalidHash:         BCrypt::Errors::InvalidHash: invalid hash
            app/controllers/sessions_controller.rb:9:in `create'
            test/test_helper.rb:32:in `log_in_as'
            test/controllers/users_controller_test.rb:36:in `block in <class:UsersControllerTest>'

  35/35: [=====================================================] 100% Time: 00:00:01, Time: 00:00:01

Finished in 1.71823s
35 tests, 87 assertions, 0 failures, 2 errors, 0 skips

测试/装置/ users.yml里

michael:
  name: Michael Example
  email: michael@example.com
  password_digest: <%= User.digest('password') %>

archer:
  name: Sterling Archer
  email: duchess@example.gov
  password_digest: <%= User.digest('password') %

测试/控制器/ users_controller_test.rb

require 'test_helper'

class UsersControllerTest < ActionDispatch::IntegrationTest

  def setup
    @user = users(:michael)
    @other_user = users(:archer)
  end

  test "should get new" do
    get signup_path
    assert_response :success
  end

 test "should redirect edit when not logged in" do
    get edit_user_path(@user)
    assert_not flash.empty?
    assert_redirected_to login_url
  end

  test "should redirect update when not logged in" do
    patch user_path(@user), params: { user: { name: @user.name,
                                              email: @user.email } }
    assert_not flash.empty?
    assert_redirected_to login_url
  end

   test "should redirect edit when logged in as wrong user" do
    log_in_as(@other_user)
    get edit_user_path(@user)
    assert flash.empty?
    assert_redirected_to root_url
  end

  test "should redirect update when logged in as wrong user" do
    log_in_as(@other_user)
    patch user_path(@user), params: { user: { name: @user.name,
                                              email: @user.email } }
    assert flash.empty?
    assert_redirected_to root_url
  end

end

应用程序/控制器/ users_controller.rb

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)
  #  @user = User.new(params[:user])    # Not the final implementation!
    if @user.save
      log_in @user
      flash[:success] = "Welcome to the Sample App!"
      redirect_to @user
      # Handle a successful save.
    else
      render 'new'
    end
  end

  def edit
    # can take out because already in correct_user     @user = User.find(params[:id])
  end

  def update
    # can take out because already in correct_user @user = User.find(params[:id])
    if @user.update_attributes(user_params)
      # Handle a successful update.
      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?
        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 @user == current_user
    end

end

为什么当本书说所有测试都应该通过时,我会得到2次失败的测试?我已经查看了错误消息提到的行,但我不确定是什么问题。 谢谢你的帮助。

编辑:会话控制器

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) 
    redirect_to @user

    else
    flash.now[:danger] = 'Invalid email/password combination' # Not quite right!
    render 'new'
    end
  end

  def destroy
    log_out if logged_in?
    redirect_to root_url
  end
end

2 个答案:

答案 0 :(得分:1)

你在“Sterling Archer”测试夹具的password_digest中缺少一个闭合角括号。

password_digest: <%= User.digest('password') %

应该是

password_digest: <%= User.digest('password') %>

答案 1 :(得分:0)

在线上更加专注:

BCrypt::Errors::InvalidHash: invalid hash
            app/controllers/sessions_controller.rb:9:in `create'

您没有指定此控制器的代码,我无法告诉您可能出现的问题。