Search code examples
ruby-on-railsjsonrubyrubocopruby-style-guide

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?


Solution

  • 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