Summer Sale! All accounts are 50% off this week.

pio.skro@gmail.com's avatar

Catch unfaked Http requests

I am working on application that rely on many external apis.

All the tests should be based on mocks by facade Http and method Http::fake()

Is it possible to throw an exception if any request to external service is not covered by Http::fake()?

I know how to resolve it by global middleware. But I am looking for any resolution in TestCase::setUp()

0 likes
2 replies
ramonrietdijk's avatar
Level 30

Yes, it is possible to prevent these HTTP calls using the method preventStrayRequests which has been added since Laravel 9.

Http::preventStrayRequests();

If a requested URL is not faked, the test will throw an exception.

RuntimeException: Attempted request to [laracasts.com] without a matching fake.

Please or to participate in this conversation.