Skip to content
Advertisement

How to make Laravel’s Notification throw an Exception in test

I have a few laravel commands that inherit from my own class to send slack messages if they fail. However if the slack notification fails I still want the original exception thrown so that the error still ends up in the logs if slack is unavailable or misconfigured. I have this and it works, but I can’t figure out how to trigger an exception in the Notification part in tests.

JavaScript

As Notification::route is defined on the facade I can’t use Notification::shouldReceive to trigger the exception and the SlackNotification is newed up making it difficult to mock.

Any ideas as to how I can trigger an exception?

Advertisement

Answer

If anybody’s still wondering how to do this, you can mock Notification::send like:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement