2016-09-16 18 views
-1

我在我的測試文件中遇到了Rubocop問題。首先,這是我現在的代碼:Rubocop JSON:對齊方法調用的參數(如果它們跨越多條線)

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每次都拋出相同的錯誤。安迪順便說一下,把整個JSON放在一條線上也不是解決方案。 任何人都可以解釋一下,正確的排列方式是什麼樣子,以便Rubocop對它滿意嗎?

+0

'headers:@ header'不是JSON,它是等同於{{headers => @header}的Ruby哈希值'作爲第二個參數傳遞給' GET'。 –

回答

0

將其更改爲

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)是你的第一個參數,以獲得第二PARAM headers: @header應該與它

同樣對齊適用於在這裏你有三個PARAMS第二位

相關問題