Dependeny Injection on Integration Tests

Hello!

How can I inject dependencies in the controller that I will test in integration tests?
I would like something similar to this in an integration test:

Inject Dependency on FooController Then
$this->post('/foo/action');

I would inject a mocked service class…

I’m currently doing it using events:

$service = $this->createMock(MyService::class);
EventManager::instance()->on(
  'Controller.startup',
  function ($event) use($service) {
      $event->getSubject()->_setServiceObject($service);
  });

$this->post('/controller/action', $notificationString);

But it doesn’t seem like an elegant solution.

Does the answer to this question help? I’m not sure if the controller spy will work for your scenario or not.