#Assert json structure from external api call
14 messages · Page 1 of 1 (latest)
No, HTTP tests are used to test the current application, as described on the first line in the docs:
Laravel provides a very fluent API for making HTTP requests to your application
https://laravel.com/docs/9.x/http-tests
thanks for the reply!
so what would u do in this scenario?
Why would you test an external API over which you have no control? In your tests you'd mock calls to that API so you don't actually hit the API. There's a bit of "trust" there, as you wouldn't expect the API to randomly change
that is exactly what i'm checking, if the external api changes. so that is why i want to assert there is some keys. so if they change it, as my tests will fail, ill get notified that the api changed
it's important for me to check that
Yeah, and if they change the API while you haven't deployed/committed for 3 weeks because of holiday you wouldn't get notified either? 
That's just what proper exception handling is for. You'll get notified of exceptions, you'll show nice error messages to your users. Verifying an external API in tests is basically useless in this case, because if you tests fail then the API was already changed, so your users would already experience issues and you should already have gotten exception messages
this external api call is executed at late night. the idea is to run the tests there. if they fail, the call won't be executed.
pros: the users will not see any errors
cons: some data will not be updated (till i fix it)
Yeah, that's not what tests are for, they're to test your code. So if you have some sort of cron running at night, and the api somehow magically changed, you'll get errors and things won't get executed. Use DTOs to verify the incoming data etc. It'll fail if things aren't correct. Setup error logging so you'll get notified of that
i see. im not familiar with DTOs yet. im going to search about them