应创建用户并应在 has_secure_password 后更新用户错误

should create user and should update user error after has_secure_password

我是 Ruby 的新手,正在尝试通过一些教程来设置用户身份验证。

我使用脚手架创建用户模型/控制器/视图/测试,其中 password_digest 列已经就位。

当我添加 has_secure_password 属性时,它破坏了控制器创建用户和更新用户测试,我似乎无法弄清楚如何修复它们。

这里是测试代码test/controllers/users_controllers_test.rb:

require 'test_helper'

class UsersControllerTest < ActionController::TestCase
  setup do
    @user = users(:one)
  end

  test "should create user" do
    assert_difference('User.count') do
      post :create, user: { email: @user.email, password: @user.password, password_confirmation: @user.password_confirmation, user_name: @user.user_name }
    end

    assert_redirected_to user_path(assigns(:user))
  end

  test "should update user" do
    patch :update, id: @user, user: { email: @user.email, password: @user.password, password_confirmation: @user.password_confirmation, user_name: @user.user_name }
    assert_redirected_to user_path(assigns(:user))
  end
end

控制器代码如下:

class UsersController < ApplicationController
  before_action :set_user, only: [:show, :edit, :update, :destroy]

  def new
    @user = User.new
 end

  def edit
  end

  def create
    @user = User.new(user_params)

    respond_to do |format|
      if @user.save
        format.html { redirect_to @user, notice: 'User was successfully created.' }
        format.json { render :show, status: :created, location: @user }
      else
        format.html { render :new }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @user.update(user_params)
        format.html { redirect_to @user, notice: 'User was successfully updated.' }
       format.json { render :show, status: :ok, location: @user }
     else
        format.html { render :edit }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
   end
 end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_user
      @user = User.find(params[:id])
    end

   # Never trust parameters from the scary internet, only allow the white list through.
   def user_params
     params.require(:user).permit(:user_name, :email, :password, :password_confirmation)
   end
end

这是我的 users.yml 夹具文件:

one:
  user_name: MyString
  email: MyString
  password_digest: MyString

two:
  user_name: MyString
  email: MyString
  password_digest: MyString

我得到的两个失败是: 失败 ["test_should_create_user",UsersControllerTest,0.5491727360058576] test_should_create_user#UsersControllerTest(0.55 秒) "User.count" 没有变化 1。 预期:3 实际:2 test/controllers/users_controller_test.rb:15:in `block in '

失败["test_should_update_user",UsersControllerTest,0.5988616980030201] test_should_update_user#UsersControllerTest(0.60 秒) 预期响应为 ,但为 <200> test/controllers/users_controller_test.rb:34:在`块中'

我有什么想法可以让它通过吗???

谢谢!


编辑:

我一直在摆弄并且能够将 users.yml 文件修改为:

one:
  user_name: "name"
  email: "USER@EXAMPLE.INC"
  password_digest: "a$tAS/0m5JGW.k0o/h.eYwrOx6UTfJCmasKnRE0tS3kAqZWGMCZ.jba"

并且我添加了 puts assigns(:user) 的日志记录。errors.inspect

我列出了以下错误:

#<ActiveModel::Errors:0x007f8a1f0935e8 
@base=#<User id: 980190962, 
user_name: "name", 
email: "USER@EXAMPLE.INC", 
password_digest: "a$tAS/0m5JGW.k0o/h.eYwrOx6UTfJCmasKnRE0tS3kAq...", 
created_at: "2016-04-08 20:14:16", 
updated_at: "2016-04-08 20:14:16">, 
@messages={:password=>["can't be blank", "is too short (minimum is 6 characters)"], :user_name=>[], :email=>[], :password_confirmation=>[]}>

但是当我尝试将密码:属性添加到我的 users.yml 文件时,它使我的所有测试都出错并显示错误:table "users" 没有命名的列"password".

有什么想法吗??

首先不要基于固定装置创建用户。夹具在测试开始时插入数据库。

因此:

  1. 将夹具 :one 恢复为原始形状
  2. 将测试代码更改为:

需要'test_helper'

class UsersControllerTest < ActionController::TestCase

  setup do
    @user = users(:one)
  end

  test "should create user" do    
    assert_difference('User.count') do
      post :create, user: { email: "test_123@example.com", password: 'testpassword', password_confirmation: 'testpassword', user_name: 'User name' }
    end

    assert_redirected_to user_path(assigns(:user))
  end

  test "should update user" do
    patch :update, id: @user, user: { email: @user.email, password: 'testpassword', password_confirmation: 'testpassword', user_name: @user.user_name }
    assert_redirected_to user_path(assigns(:user))
  end
end