简体   繁体   English

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

[英]Michael Hartl's tutorial Chapter 10.2.2, listing 10.26 tests failing

Going through Hartl's tutorial and have searched for an answer, but found none. 浏览Hartl的教程并搜索了答案,但没有找到答案。 (There may be answers existing but becuase the chapters have been updated and switched around in the newer editions I can't find them if they exist.) (可能存在答案,但由于各章已在较新版本中进行了更新和切换,如果存在,则找不到它们。)

I am at section 10.2.2. 我在第10.2.2节。 listing 10.26 doing the rails test. 清单10.26做了rails测试。

here is the error for my tests: 这是我的测试错误:

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

test/fixtures/users.yml 测试/装置/ 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') %

test/controllers/users_controller_test.rb 测试/控制器/ 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

app/controllers/users_controller.rb 应用程序/控制器/ 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

Why am I getting 2 failed tests when the book says that all tests should pass? 当书上说所有测试都应该通过时,为什么我会出现2个失败的测试? I have looked at the lines the error message mentions ,but I am not sure what is wrong. 我查看了错误消息中提到的内容,但是我不确定什么地方出错了。 Thanks for the help. 谢谢您的帮助。

EDIT: Sessions 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
    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

You're missing a closing angle bracket in the password_digest for the "Sterling Archer" test fixture. 您在“ Sterling Archer”测试装置的password_digest缺少一个闭合的尖括号。

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

should be 应该

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

look more attentively at line: 更专心地看一看:

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

You did not specify the code for this controller, and I can not tell you what might be wrong. 您未指定此控制器的代码,我也无法告诉您可能出了什么问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM