Using RSpec's mock/stub, how do I write a unit test for the find_by_id method?
I want to use RSpec, not WebMock or VCR. How do I create a stub for the request/response?
class RapidApiClient
HOST_URL = 'https://brianiswu-open-brewery-db-v1.p.rapidapi.com/breweries'
API_KEY = 'private_api_key'
def request_api(url)
Excon.get(
url,
headers: {
'X-RapidAPI-Host' => HOST_URL,
'X-RapidAPI-Key' => 'API_KEY'
}
)
end
def find_by_id(id)
response = request_api("#{HOST_URL}/#{id}")
return nil if response.status != 200
JSON.parse(response.body)
end
end
The response is:
[
{"id":4 , "name":"Ban Brewing Company" , "brewery_type":"micro", "city":"Tulsa" , "state":"OK"}
{"id":44,"name":"Tab Brewing" "brewery_type":"micro", "city":"Birmingham", "state":"MO"}
]