簡體   English   中英

如何修復“沒有將字符串隱式轉換為哈希”

[英]How to fix "no implicit conversion of String into Hash"

我正在嘗試使用 RSpec 運行測試,但它總是返回相同的錯誤:

DevicesController#update when user is a manager must update any device
     Failure/Error: put device_path device_id, params: params, headers: { 'Content-Type': 'application/vnd.api+json' }

     TypeError:
       no implicit conversion of String into Hash

我的請求參數函數:

def update_params(device, device_id)
  {
    data: {
      id: device.id,
      type: 'device',
      attributes: {
        device_id: device_id 
      }
    }
  }.to_json
end

我使用工廠的測試:

context 'when user is a manager' do
  let(:user) { create(:manager).account }
  let(:firebase_token) { Faker::Crypto.sha1 }
  let(:device_id) { physical_device.id }
  let(:params) { update_params(physical_device, firebase_token) }
  it 'must update any device' do
    expect(Device.find(device_id).device_id).to eq(firebase_token)
  end
end

請求操作系統更新設備:

put device_path device_id, params: params, headers: { 'Content-Type': 'application/vnd.api+json' }

ActionController::TestCase期望params是一個哈希值(就像headers )。

params傳遞給這個方法調用

put device_path device_id, params: params, headers: { 'Content-Type': 'application/vnd.api+json' }

update_params方法生成,該方法對生成的哈希調用to_json ,因此返回一個 JSON字符串

只需從update_params方法的最后一行中刪除.to_json就可以了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM