2016-04-08 16 views
0

私はRubyを初めて使い、ユーザー認証を設定するためのチュートリアルを試行しています。はユーザーを作成する必要があり、has_secure_passwordの後にユーザーエラーを更新する必要があります

私はscaffoldを使用してpassword_digestカラムがすでに設定されているユーザモデル/コントローラ/ビュー/テストを作成しました。

has_secure_password属性を追加すると、コントローラーの作成ユーザーと更新ユーザーテストが壊れてしまい、修正方法がわからないようです。ここで

は、テストコードのテスト/コントローラ/ 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 

2の障害は、次のとおりです。 FAIL [ "test_should_creat e_user」、UsersControllerTest、0.5491727360058576] test_should_create_user#1 UsersControllerTest(0.55s) "User.countは、" 期待する1 によって変化しなかった:3 実績:2 テスト/コントローラ/ users_controller_test.rb:15:ブロック `で

FAIL [ "test_should_update_user"、UsersControllerTest、0.5988616980030201] test_should_update_user#UsersControllerTest(0.60s)と 期待される応答 'で、しかし< 200> テスト/コントローラ/ users_controller_test.rbであった:34: `ブロック内in '

どのようにすればいいですかこれを渡す?

ありがとうございます!


EDIT:

私がいじるとにusers.ymlファイルを変更することができたしてきた:

one: 
    user_name: "name" 
    email: "[email protected]" 
    password_digest: "$2a$10$tAS/0m5JGW.k0o/h.eYwrOx6UTfJCmasKnRE0tS3kAqZWGMCZ.jba" 

そして私はプットの割り当てのログ記録を追加(:ユーザ)。 errors.inspect

私が記載されている次のエラーを取得する:

#<ActiveModel::Errors:0x007f8a1f0935e8 
@base=#<User id: 980190962, 
user_name: "name", 
email: "[email protected]", 
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ファイルにpassword:属性を追加しようとすると、すべてのテストがエラーでエラーになります。テーブル "users"には "password"という名前の列はありません。

答えて

0

まず最初に、フィクスチャーに基づいてユーザーを作成しないでください。フィクスチャは、テスト開始時にデータベースに挿入されます。

したがって:

  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: "[email protected]", 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 
+0

[OK]を必要と!ありがとう!なぜセットアップはテストの中で行われますか?それはそうではないより多くの誤りを引き起こすように思われた。私はそれを外に置いて、作業するユーザーを更新する必要がありました。今は "ActionController :: Unknown Format"というエラーが出ますが。実際には、後でやった他のものに起因するようです。私はそれを個別に解決しようとしています。 – blueduckyy

+0

セットアップのテストは私の間違いでした - 私はそれを修正しました、申し訳ありません。 – Esse

関連する問題