I've created the following service
class ApiCustomers < ApiBase
def initialize(url: nill)
url = url.presence || ENV.fetch("CUSTOMERS_API_APP_NAME")
super(url)
end
def find_customer_apps(customer_id, params=nil)
begin
customer_url = "#{@url}/customers/#{customer_id}/applications"
# call api-customers
if params.nil?
response = connection.get(customer_url)
else
response = connection.get(customer_url, params)
end
rescue StandardError => e
Rails.logger.error "Error fetching API-CUSTOMERS Applications: #{e}"
raise e
end
return response.body["data"]
end
end
Now, I want to test that with the following code:
require "rails_helper"
RSpec.describe ApiCustomers do
let(:customer_id) { SecureRandom.uuid }
let(:customers_client) { ApiCustomers.new(url: "api_customers.local") }
context "#find_customer_apps" do
let(:app) { double("application", id: 123, name: "App", customer_id: "123", supported: false, additional_notes: "Test") }
let(:success_response) { OpenStruct.new({ status: 200, body: { "data": app } }) }
let(:bad_request) { OpenStruct.new({ status: 400, body: {"error"=>"invalid call"} }) }
describe "with valid params" do
before do
allow_any_instance_of(Faraday::Connection).to receive(:get).and_return(success_response)
end
it "returns customer applications" do
applications_payload = customers_client.find_customer_apps(account_id, nil)
expect(applications_payload).not_to be_nil
end
end
describe "with invalid params" do
before do
allow_any_instance_of(Faraday::Connection).to receive(:get).and_raise(CustomErrors::BadRequest)
end
it "raises an error" do
expect { customers_client.find_customer_apps(nil, nil) }.to raise_error(CustomErrors::BadRequest)
end
end
end
end
The test with invalid params
is working, but for some reason the one with valid params
is failing.
If I print the success_response
, this is being created properly. I do not know what I am getting nil
in my applications_paylod
. What I am missing? I thought this line should cover what I am doing: allow_any_instance_of(Faraday::Connection).to receive(:get).and_return(success_response)
Thanks in advance.