I have a job class which purpose is to send some user data to HubSpot, and to do that it uses Guzzle client to call the create or update API endpoint since the HS PHP client seems to lack this feature.
The problem is that during job testing I don't want to really call HS API, so I need a way to mock the Guzzle client from the outside of the job, but I can't find none.
I've also thought to add the client as a constructor arg and create the job via the service container to take advantage of Laravel's dependancy injection system, but I've discarded the idea since the job is never called/created in the application via service container and it therefore require to update the entire application where needed.
How can I test the job without really making a call to the HS API?
CodePudding user response:
You can set Http::fake method in your tests. e.g.:
Http::fake([
// Stub a string response for Google endpoints...
'google.com/*' => Http::response('Hello World', 200, $headers),
]);
And I would pair this with my new favorite test helper for Preventing Stray Requests that will throw exeption if you have real HTTP requests called from your tests:
Http::preventStrayRequests();