Tob*_*obi 1 ruby json ruby-on-rails rubocop ruby-style-guide
我在测试文件中遇到了 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
Run Code Online (Sandbox Code Playgroud)
这就是 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: { ...
^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)
所以我在样式指南中搜索了 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
Run Code Online (Sandbox Code Playgroud)
解释:
正如user_url(@user)
您获得第二个参数的第一个参数headers: @header
应该与它对齐
同样适用于您有三个参数的第二名