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...

  1. Make a Controller:
    >php artisan make:controller SearchComics 
    
  2. 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, ...

Get Laravel 5.x Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.