JorickL
142
7
Testing

Swap class when running test?

Posted 1 month ago by JorickL

In my Laravel 5.7 I want to 'fake' an API endpoint of a service we use. I've created a 'Fake class' which acts as the API. In my models I whip up a new connection to the external API like this:

$connection = (new TicketAPI)->bookings($attributes);

This works, like a charm.

Now, I want to replace, or 'swap' the TicketAPI::class when I'm running tests. So, I thought I could use

$fakeTicket = new FakeTicket;
$this->app->instance(TicketAPI::class, $fakeTicket);

But it still reaches the TicketAPI::class. Is someone able to point me in the right direction, or am I trying to use the $this->app->instance() method in the complete wrong way? Is it even possible to swap classes like this?

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.