简体   繁体   English

Rubocop JSON:如果方法调用的参数跨越多行,则对齐它们

[英]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. 我在测试文件中遇到了Rubocop的问题。 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: 这就是Rubocop错误输出:

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. 因此,我在样式指南中搜索了JSON的正确对齐方式。 I really tried every combination of indenting and new lines, but Rubocop throws every time the same error. 我确实尝试了缩进和换行的每种组合,但是Rubocop每次都抛出相同的错误。 Andy by the way, put the whole JSON in one line is also not the solution. 顺便说一句,Andy将整个JSON放在一行中也不是解决方案。 Can anybody explain, how the correct alignment looks like, so that Rubocop is happy with it? 任何人都可以解释一下正确的对齐方式如何,从而使Rubocop满意吗?

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 由于user_url(@user)是获取第二个参数headers: @header第一个参数headers: @header应该与其对齐

Same applies for second place where you have three params 同样适用于拥有三个参数的第二名

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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