繁体   English   中英

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

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

我在测试文件中遇到了Rubocop的问题。 首先,这是我的代码:

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

这就是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: { ...
        ^^^^^^^^^

因此,我在样式指南中搜索了JSON的正确对齐方式。 我确实尝试了缩进和换行的每种组合,但是Rubocop每次都抛出相同的错误。 顺便说一句,Andy将整个JSON放在一行中也不是解决方案。 任何人都可以解释一下正确的对齐方式如何,从而使Rubocop满意吗?

更改为

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

说明:

由于user_url(@user)是获取第二个参数headers: @header第一个参数headers: @header应该与其对齐

同样适用于拥有三个参数的第二名

暂无
暂无

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

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