robertldeboer
1 week ago
171
8
Testing

API Controller Testing

Posted 1 week ago by robertldeboer

I have used $this->json in the past to test controllers - specifically to write unit test for API controllers. It is simple and when combined with it's other functions like assertStatus and such, is simple. It works for 90% of my use cases.

However I am now attempting to test an API route that results in the controller calling an external API with some of the passed data. In this circumstance I do not want the controller to run like normal - I would like to intercept the external API call and return dummy data to cut out the calls and control the result.

I realize that using my current approach will not work b/c it is calling the API as a normal HTTP request and so there is no connection between that and the running test, so I can't mock the HTTP object.

If and how are others doing something similar?

Please sign in or create an account to participate in this conversation.