Home > OS >  stub_request must return array in body
stub_request must return array in body

Time:06-30

I would like test my service with rspec but i got a error in my return body undefined method 'each' for "invoices":String because in my original method i parse an array in the response

I would like know how can i test this method and send a array in return body

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

stub request 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: {})

CodePudding user response:

Try this

.to_return(status: 200, body: '{"invoices" => [{ "invoice_id": "123"}]}', headers: {})

  • Related