Using mockery to test your controllers
We will cover a feature in Laravel to test your controllers. In doing so, I will show how to swap out the injected methods so that when your controller tries to inject the method, it will get your mock instead.
Getting ready
Since Mockery is already part of Laravel, we are ready to go! Just start gulp watch
as we talked about earlier in this chapter if you want to tests to just happen as you write.
How to do it...
Make a Controller:
>php artisan make:controller SearchComics
Now, let's go set up the Controller called
app/Http/Controllers/SearchComics.php
for our request.When you are done, your Controller will look like this:
Tip
Note that
@var MarvelApi
is a nice feature in most IDEs. Even though it is an interface, I can tell it what concrete class it is representing, and then I can click into this class and explore it.And our Route (notice we are taking over the home
route /
):Make a test for the controller:
> php artisan make...