0

更新方法的集成测试(带测试/单元):

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: 'patch@it.man',
                     name: 'Patch!',
                     password: 'qwerty',
                     password_confirmation: 'qwerty'
  assert_equal 'User updated!', flash[:notice]
end

当我运行测试时,我得到了这个错误:

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>'

我的users_controller.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当我在控制器中使用强参数时,如何测试?

4

1 回答 1

2

params.require(:user)要求:user =>参数哈希的根有一个参数。

尝试这个:

patch_via_redirect user_url(user), { user: {
                                        email: 'patch@it.man',
                                        name: 'Patch!',
                                        password: 'qwerty',
                                        password_confirmation: 'qwerty'
                                        } }

可怕的格式,但应该明白这一点。

于 2013-08-26T20:21:53.357 回答