简体   繁体   中英

Rubocop JSON: Align the parameters of a method call if they span more than one line

i got a problem with Rubocop in my testing files. At first, this is my code now:

should 'should show user' do
  get user_url(@user),
    headers: @header
  assert_response :success
end

should 'should update user' do
  patch user_url(@user),
    params: {
      data: {
        attributes: {
          name: @user.name
        }
      }
    }, headers: @header
  assert_response :success
end

And that is the Rubocop error output:

test/controllers/users_controller_test.rb:34:9: C: Align the parameters of a method call if they span more than one line.
        headers: @header
        ^^^^^^^^^^^^^^^^
test/controllers/users_controller_test.rb:40:9: C: Align the parameters of a method call if they span more than one line.
        params: { ...
        ^^^^^^^^^

So I searched in the styleguide for the correct alignment of JSON. I really tried every combination of indenting and new lines, but Rubocop throws every time the same error. Andy by the way, put the whole JSON in one line is also not the solution. Can anybody explain, how the correct alignment looks like, so that Rubocop is happy with it?

Change it to

should 'should show user' do
  get user_url(@user),
      headers: @header
  assert_response :success
end

should 'should update user' do
  patch user_url(@user),
        params: {
          data: {
            attributes: {
              name: @user.name
            }
          }
        },
        headers: @header
  assert_response :success
end

Explanation:

As user_url(@user) is your first param to get second param headers: @header should be aligned with it

Same applies for second place where you have three params

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM