我想用rspec测试我的服务,但我在返回体undefined method 'each' for "invoices":String
中得到了一个错误,因为在我的原始方法中,我在响应
中解析了一个数组我想知道如何测试此方法并在返回体中发送数组
My method in service:
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
存根请求I tried:
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: {})
在通话结束时试试这个:
.to_return(status: 200, body: '{"invoices" => [{ "invoice_id": "123"}]}', headers: {})