0

I am trying to test, updating a User with devise strong params.

Strong params are set in the application controller:

devise_parameter_sanitizer.for(:account_update) do |u|
  u.permit(:email, :password, :password_confirmation, :current_password,
           :time_zone)
  end

The Registrations Controller update action is:

def update
  user_update_params = devise_parameter_sanitizer.sanitize(:account_update)
  ....
  @user = User.find(current_user.id)
  ....
  @user.update(user_update_params)
  ...
end

In my test I am passing update attributes:

def update_attributes
  {user: {email: 'bob@home.com',
  time_zone: 'Melbourne'}}
end

test 'Update' do
  patch(:update, update_attributes)
  assert_equal 'bob@home.com', assigns(:user).email
  assert_equal 'false', assigns(:user).use_weighted_rates
end

The problem is the Devise::ParameterSanitizer object @params reflects the @user attributes, not the update_attributes:

@params=
  {"user"=>
    {"email"=>"john@home.com",
     "time_zone"=>"Sydney"}}

So the test assertions fail.

What am I doing wrong?

  • What is the error? – ruby_newbie Jun 19 '16 at 02:35
  • The test is not passing: assert_equal 'bob@home.com', assigns(:user).email Minitest::Assertion: Expected: "bob@home.com" Actual: "john@home.com" The problems is that user_update_params are: {"email"=>"john@home.com", "time_zone"=>"Sydney"} – Philip Hancock Jun 19 '16 at 02:35
  • What about when you change this line: "@user.update(user_update_params)" to "@user.update!(user_update_params)". Does it raise an exception? – ruby_newbie Jun 19 '16 at 02:39
  • No, because there is no problem with the user_update_params. The problem is the devise sanitizer is returning the attributes from the user, not from the params passed to the controller – Philip Hancock Jun 19 '16 at 02:42

1 Answers1

1

The problem was the user was not signed in. I was creating a user using: get(:create) to create a user on the create action of the controller. I changed this is to sign_in create(:user) using a user factory and all good.