From the course: Building a Website with Laravel, React.js, and Inertia

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

Solution: Testing API Endpoints with PHPUnit

Solution: Testing API Endpoints with PHPUnit - Laravel Tutorial

From the course: Building a Website with Laravel, React.js, and Inertia

Solution: Testing API Endpoints with PHPUnit

(upbeat music) - [Instructor] How did you do? Did you manage to write one, two, or maybe five tests? You may have struggled, and that's okay too. Writing tests is not easy. Let me show you what I did. Again, these tests will be very basic and only test the happy path because there could be and probably is a whole course on testing, so spending more time on tests is beyond the scope of this course. I'm not going to type all tests out. To save time, I'll paste them in and we will go over them together. In this first test, I generate 10 fake posts, perform a Get request to the post or index route, and assert that the response is a status code of 200, and that it contains exactly 10 items inside the data key. In the second test, I'm creating a new post. Then I pass the post to the posted store route using the JSON post method. Then I assert that I get two and one response back and that the JSON contain the correct payloads. Finally, I'm asserting that the post table in the database has…

Contents