Question

Integration test (with Test/Unit) of update method:

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

When I run the test I got this error:

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

Functions in my 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

How to test the patch, when I use strong parameters in my controller?

Was it helpful?

Solution

params.require(:user) requires there to be a :user => param at the root of the parameters hash.

Try this:

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

Terrible formatting, but should get the point across.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top