2017-10-30 10 views
0

私は、ユーザーのタイトルが保存されていることをチェックするRails統合テストを作成しています。タイトルには1つのバリデーションがあります。これは255文字以内でなければなりません。しかし、@user.update_attributes!(title: params[:title])は、「パスワードは6文字以上でなければならない」というエラーを投げています。しかし...私はパスワード以外のタイトルを更新していません。では、この属性を独自の検証で保存し、パスワードを心配する必要はありません。(Rails)update_attributesは、パスワードを使用しない統合テスト中にエラー(パスワードが必要)をスローします

テスト:

test "profile submits new title and description successfully" do 
    log_in_as(@non_admin) 
    get user_path(@non_admin) 
    assert_nil @non_admin.title 
    post "https://stackoverflow.com/users/#{@non_admin.id}/update_description", 
     { title: "I am a man of constant sorrow." } 
    user = assigns(:user) 
    user.reload.title 
    assert user.title == "I am a man of constant sorrow." 
    assert_template 'users/show' 
    assert flash[:success] 
    end 

コントローラのメソッドは(終了していないが、あなたのアイデアを得るでしょう)。パスワードの検証エラーをスローするのはupdate_attributes!コールです。

# Handles user's posted title and description. 
    def update_description 
    @user = User.find(params[:id]) 
    # Check if title is present. If so, attempt to save, load flash, and reload. 
    if @user.update_attributes!(title: params[:title]) 
     flash[:success] = "Saved title. " 
    # if unable, set error flash and reload. 
    else 
     flash[:warning] = "Unable to save." 
    end 
    # Same logic as before, now for description. 
    # Make sure two different [:success] flashes work! Probably not! 
    redirect_to user_path(@user) 
    end 

検証:ここ

validates :password, length: { minimum: 6, 
           message: "must have at least 6 characters" } 
    validates :title, length: { maximum: 255 } 

は、テストエラーがあります:

23:08:51 - INFO - Running: test/integration/users_show_test.rb 
Started 
ERROR["test_profile_submits_new_title_and_description_successfully", UsersShowTest, 2017-10-23 01:06:11 -0400] 
test_profile_submits_new_title_and_description_successfully#UsersShowTest (1508735171.57s) 
ActiveRecord::RecordInvalid:   ActiveRecord::RecordInvalid: Validation failed: Password must have at least 6 characters 
      app/controllers/users_controller.rb:71:in `update_description' 
      test/integration/users_show_test.rb:22:in `block in <class:UsersShowTest>' 
     app/controllers/users_controller.rb:71:in `update_description' 
     test/integration/users_show_test.rb:22:in `block in <class:UsersShowTest>' 

ケースでは、関連ですが、ここでは@non_adminとしてロードされ、固定具だ:

archer: 
    name: Sterling Archer 
    email: [email protected] 
    password_digest: <%= User.digest('Jsdfuisd8f') %> 
    activated: true 
    activated_at: <%= Time.zone.now %> 

私は'm a Raそれはおそらく何か基本的なものです。事前に感謝...

更新:下記のkasperiteとの議論を参照してください。パスワードの検証にon: createを追加するだけでした。

答えて

1

update_attributes!を呼び出すとsave!がトリガされ、モデルの検証がトリガされます。パスワードを入力しないと、例外がスローされます。

あなたはどちらか検証 またはこれをバイパスされupdate_attribute(:title, params[:title])を行うことができます。

@user.title = params[:title] 
    @user.save!(validation: false) 

参照:返信用http://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-update-21

+0

感謝を! 問題は、この検証をトリガーしたいと思っていますが、この属性のみです。私は 'update_attributes!'が 'save!'をトリガすることを知っています(データベースに保存したい、それは私がやろうとしていることです)。関連する検証だけを利用する方法はありますか? 統合テストを行うには、私は自分の設備を別に用意する必要がありますか?上のフィクスチャを見せてください(おそらく関連性がありますか?)。 – globewalldesk

+1

検証をトリガするタイミングを制御する唯一の方法は、モデル(http://guides.rubyonrails.org/active_record_validations.html#on)です。私はそれがあなたが望むものだと推測します。 – kasperite

+0

OKです。それは完全に修正されました: 'validates:パスワード、長さ:{最小:6、メッセージ:少なくとも6文字以上}}:create' それで簡単です。ありがとう、相棒! – globewalldesk

関連する問題