title: Mocking a dependency injection in a trait or a model
body: <p>I'm trying to mock a dependency injection or (would be better) a model relationship.<br>
  The model <code>Place</code> as a <code>morphTo</code> relationship with few modules called <code>Placeable</code>.
  The modules/placeables can <code>detect</code> the proximity of the user. One of them is using a <code>RemoteGateway</code> which is specific to the module.</p>
  
  <p>The model that use the <code>RemoteGateway</code> has this method</p>
  
  <pre><code>use UseRemote;
  public function detect(Geolocalisable $geolocalisable): bool
  {
      return $this-&gt;remote()-&gt;recognize($geolocalisable);
  }
  </code></pre>
  
  <p>and use this trait</p>
  
  <pre><code>trait UseRemote
  {
      public static $remote;
  
      protected static function bootUseRemote()
      {
          self::$remote = app(RemoteGateway::class);
      }
  
      public function remote(): RemoteGateway
      {
          return self::$remote;
      }
  }
  </code></pre>
  
  <p>And of course the RemoteGateway is binded</p>
  
  <pre><code>$this-&gt;app-&gt;bind(RemoteGateway::class, function ($app) {
      return new SomethingRemoteGateway();
  });
  </code></pre>
  
  <p>Since <code>remote()-&gt;recognize($user)</code> depend on an external service, it will mostly always return <code>false</code> but for the tests I have to assume, it detect him and should always return true.</p>
  
  <p>I'm now trying to mock this behavior but it doesn't work.</p>
  
  <pre><code>    /** @test */
      public function api_show_place_with_registrables: void
      {
          $user = factory(User::class)-&gt;create();
  
          // mocking Remote Gateway
          $remoteGateway = \Mockery::mock(RemoteGateway::class);
          $remoteGateway-&gt;shouldReceive('recognize')
              -&gt;with($user)
              -&gt;andReturn(true);
          $this-&gt;app-&gt;instance(RemoteGateway::class, $remoteGateway);
  
  
          $place = factory(Place::class)-&gt;create();
  
          $response = $this-&gt;actingAs($user, 'api')
              -&gt;json('get', "/api/v1/places/{$place-&gt;id}");
  
          dd($response-&gt;getContent()); // &lt;-- empty but when I hardcode true in SomethingRemoteGateway it's returning the values
      }
  </code></pre>
  
  <p>But mocking <code>Placeable</code> to always return <code>true</code> when using <code>detect</code> would be much better. In this scenario, my tests wouldn't depend of one of the module specificity.</p>
  
  <p>An alternative I think of, would be to create a FakePlaceable to use in my factories (I'm iterating all the known placeables modules and pick one atm)</p>