briankidd
1 week ago
244
4
Testing

Prevent other listeners during tests

Posted 1 week ago by briankidd

For event listeners, I include a test to ensure I have the listener registered for the event - here's an example:

/** @test */
    function was_called_when_stripe_invoice_payment_succeeded_fired()
    {
        $listener = Mockery::spy(ClearConversionRequests::class);
        app()->instance(ClearConversionRequests::class, $listener);

        event(new StripeInvoicePaymentSucceeded($this->user->currentTeam, $this->cashierInvoice));

        $listener->shouldHaveReceived('handle')->with(Mockery::on(function ($event) {
            return $event->team->is($this->user->currentTeam);
        }));

    }

I don't fake the event in this case because I'm testing for the listener being triggered. However, I don't want other listeners that are registered for this event to fire. What is the best way to do this? In the past, I've also mocked those listeners and registered them in the container but I want to believe there is a better way. Thanks

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