我是Ruby的新手,并且正在尝试通过一些教程来设置用户身份验证。
我使用了一个脚手架来创建用户模型/ controller / views / tests并使用了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
这是我的fixture.yml文件夹具:
one:
user_name: MyString
email: MyString
password_digest: MyString
two:
user_name: MyString
email: MyString
password_digest: MyString
我得到的两个失败是: 失败[&#34; test_should_create_user&#34;,UsersControllerTest,0.5491727360058576] test_should_create_user #UsersControllerTest(0.55s) &#34; User.count&#34;没有改变1。 预计:3 实际:2 test / controllers / users_controller_test.rb:15:在`block in&#39;
失败[&#34; test_should_update_user&#34;,UsersControllerTest,0.5988616980030201] test_should_update_user#UsersControllerTest(0.60s) 预期的响应是a,但是&lt; 200&gt; test / controllers / users_controller_test.rb:34:在`block in&#39;
任何想法我怎么能通过???
谢谢!
编辑:
我一直在摆弄,并且能够将users.yml文件修改为:
one:
user_name: "name"
email: "USER@EXAMPLE.INC"
password_digest: "$2a$10$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: "$2a$10$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&#34; users&#34;没有列名为&#34;密码&#34;。
任何想法?
答案 0 :(得分:0)
首先,不要根据灯具创建用户。在测试开始时将夹具插入数据库。
因此:
:one
恢复为原始形状要求&#39; test_helper&#39;
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