title: Mocking GuzzleHttp\Client for testing Lumen route
body: <p>I have the a simple Lumen application using the following two files, that upon requesting the root route <code>/</code> it makes a request to get a URL using <code>GuzzleHttp\Client</code>:</p>
  
  <p><code>routes/web.php</code></p>
  
  <pre><code>&lt;?php
  
  $router-&gt;get('/', ['uses' =&gt; 'MyController@index', 'as' =&gt; 'index']);
  </code></pre>
  
  <p><code>app/Http/Controllers/MyController</code></p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers;
  
  use GuzzleHttp\Client;
  use Illuminate\Http\Request;
  
  class MyController extends Controller {
      protected $client;
  
      public function __construct() {
          $this-&gt;client = new Client();
      }
  
      public function index( Request $request ) {
          $response = $this-&gt;client-&gt;request( 'get', 'https://www.google.com/' );
          return Response()-&gt;json( [ 'status' =&gt; $response-&gt;getStatusCode() ] );
      }
  }
  </code></pre>
  
  <p>However, I want to write a test in which <code>$response-&gt;getStatusCode()</code> will return <code>444</code>, so I wrote the following test, trying to mock <code>getStatusCode()</code> method in <code>GuzzleHttp\Client</code>:</p>
  
  <pre><code>&lt;?php
  
  use GuzzleHttp\Client;
  
  class MyTest extends TestCase {
      protected $instance;
  
      public function testMyRoute() {
          $client = Mockery::mock( Client::class )
                           -&gt;shouldReceive( 'getStatusCode' )
                           -&gt;once()
                           -&gt;andReturn( 444 );
          $this-&gt;app-&gt;instance( Client::class, $client );
  
          $this-&gt;json( 'get', '/' )-&gt;seeJson( [ 'status' =&gt; 444 ] );
      }
  
      public function tearDown() {
          parent::tearDown();
          Mockery::close();
      }
  }
  </code></pre>
  
  <p>But running <code>phpunit</code> fails:</p>
  
  <pre><code>~/experiment/lumen-test/testing » phpunit                                                                                                                                 
  PHPUnit 5.7.27 by Sebastian Bergmann and contributors.
  
  F                                                                   1 / 1 (100%)
  
  Time: 868 ms, Memory: 14.00MB
  
  There was 1 failure:
  
  1) MyTest::testMyRoute
  Unable to find JSON fragment ["status":444] within [{"status":200}].
  Failed asserting that false is true.
  
  ~/experiment/lumen-test/testing/vendor/laravel/lumen-framework/src/Testing/Concerns/MakesHttpRequests.php:288
  ~/experiment/lumen-test/testing/vendor/laravel/lumen-framework/src/Testing/Concerns/MakesHttpRequests.php:213
  ~/experiment/lumen-test/testing/tests/MyTest.php:15
  
  FAILURES!
  Tests: 1, Assertions: 2, Failures: 1.
  </code></pre>
  
  <p>That indicates that mocking did not take any effect.  What am I missing here?</p>
  
  <h3>Edit -1</h3>
  
  <p>After modifying <code>MyController</code> constructor as per @sam's comment <a href="https://stackoverflow.com/questions/49757488/mocking-guzzlehttp-client-for-testing-lumen-route/49757710#49757710">Mocking GuzzleHttp\Client for testing Lumen route</a>, I modified the test to be as follows:</p>
  
  <pre><code>use GuzzleHttp\Psr7\Response;
  </code></pre>
  
  <p>-</p>
  
  <pre><code>public function testMyRoute() {
      $response = new Response(444);
      $client = Mockery::mock( Client::class )
                       -&gt;makePartial()
                       -&gt;shouldReceive( 'request' )
                       -&gt;once()
                       -&gt;andReturn( $response );
      $this-&gt;app-&gt;instance( Client::class, $client );
  
      $this-&gt;json( 'get', '/' )-&gt;seeJson( [ 'status' =&gt; 444 ] );
  }
  </code></pre>