(Rails) update_attributes에서 비밀번호를 사용하지 않는 통합 테스트 중에 오류 (비밀번호 필요)가 발생 함

지구본

사용자의 제목이 저장되는지 확인하는 Rails 통합 테스트를 작성 중입니다. 제목에는 하나의 유효성 검사가 있습니다. 255자를 넘지 않아야합니다. 그러나 @user.update_attributes!(title: params[:title])"암호는 6 자 이상이어야합니다."라는 오류가 발생합니다. 하지만 ... 나는 암호 나 제목 이외의 다른 것을 업데이트하지 않습니다. 그렇다면이 속성을 자체 유효성 검사와 함께 저장하고 암호에 대해 걱정하지 않으려면 어떻게해야합니까?

테스트:

  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 "/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 %>

저는 Rails 멍청이이므로 아마도 기본적인 것입니다. 미리 감사드립니다 ...

업데이트 : 아래 kasperite와의 토론을 참조하십시오. on: create암호 확인 에 추가 하기 만하면 됩니다.

Kasperite

호출 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

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관