0
更新方法的

集成測試(含測試/單位):測試:patch_via_redirect和strong_parameters

test "do the patch" do 
    user = users(:alex) 
    get signin_url 
    assert_response :success 
    post_via_redirect signin_path, email: user.email, password: 'qwerty' 
    assert_equal profile_path, path 

    get edit_user_url(user) 
    patch_via_redirect user_url(user), 
        email: '[email protected]', 
        name: 'Patch!', 
        password: 'qwerty', 
        password_confirmation: 'qwerty' 
    assert_equal 'User updated!', flash[:notice] 
end 

當我跑我得到這個錯誤的測試:在我users_controller

1) Error: 
UserFlowsTest#test_do_the_patch: 
ActionController::ParameterMissing: param not found: user 
    app/controllers/users_controller.rb:43:in `user_params' 
    app/controllers/users_controller.rb:31:in `update' 
    test/integration/user_flows_test.rb:124:in `block in <class:UserFlowsTest>' 

功能。 RB

def update 
    @user = User.find(params[:id]) 
    if @user.update_attributes(user_params) 
    flash[:success] = t('activecontroller.actions.user.updated') 
    sign_in @user 
    redirect_to @user 
    else 
    render :edit 
    end 
end 

private 

    def user_params 
    params.require(:user).permit(:name, :email, :password, :password_confirmation) 
    end 

如何測試patch,當我在我的控制器中使用強參數?

回答

2

params.require(:user)要求在參數散列的根部有一個:user =>參數。

試試這個:

patch_via_redirect user_url(user), { user: { 
             email: '[email protected]', 
             name: 'Patch!', 
             password: 'qwerty', 
             password_confirmation: 'qwerty' 
             } } 

可怕的格式,而是應該傳達出點。

+0

就是這樣!謝謝! –