我想用 rspec 測驗我的服務,但我的回傳正文中出現錯誤,undefined method 'each' for "invoices":String因為在我的原始方法中,我在回應中決議了一個陣列
我想知道如何測驗此方法并在回傳正文中發送一個陣列
我的服務方法:
def generate_invoices
invoices_ids = []
response = HTTParty.get('https://books.zoho.com/api/v3/invoices?organization_id='\
"#{ENV['ZOHO_ORGANISATION_ID']}&cf_lc_contract_id_startswith=#{@contract_id}&"\
'cf_facture_final=true', headers: { 'Authorization' => "Zoho-oauthtoken #{@access_token}" })
response.code == 200 ? response : raise_error(response.body)
response['invoices'].each do |invoice|
invoices_ids.push invoice['invoice_id']
end
invoices_ids.join(',')
end
我試過的存根請求:
stub_request(:get, 'https://books.zoho.com/api/v3/invoices?cf_facture_final=true'\
"&cf_lc_contract_id_startswith=123&organization_id=#{ENV['ZOHO_ORGANISATION_ID']}")
.with(headers: { 'Authorization' => 'Zoho-oauthtoken access_token' })
.to_return(status: 200, body: { 'invoices' => [{ "invoice_id": '123' }] }.to_json,
headers: {})
uj5u.com熱心網友回復:
嘗試這個
.to_return(status: 200, body: '{"invoices" => [{ "invoice_id": "123"}]}', headers: {})
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/497647.html
