ludo237
372
4
Testing

Help figuring out how to test on demand notification

Posted 1 year ago by ludo237

Hello Everyone.

I have a slack notification class that sends a message inside our company slack, in a specific channel, every time an user performs the activation process.

The system works, but it's manually tested and that's not cool.

The notification is sent by a listener attached to an UserHasBeenActivated event, the listener is the following:

    public function handle(UserHasBeenActivated $event)
    {
        Notification::route("slack", config("services.slack.user.url"))
            ->notify(new UserActivated($event->user));
    }

Pretty straight forward. The problem here is that the notification is on demand thus it's difficult to test... because there isn't any sort of documentation on how to test on demand notification ??

At the moment I'm stuck here:

    public function it_sends_a_notification_when_an_user_is_activated()
    {
        Notification::fake();
        
        $user = factory(User::class)->states("deleted")->create();
        $user->activate();
        
        Notification::assertSentTo(
            $user,
            UserActivated::class
        );
    }

Of course this fails, the activate() method is what triggers the Event UserHasBeenActivated and sequentially all the listeners, and one of them sends the corresponding notification.

Do you know how to test on demand Notifications? Is there any hidden API that am I missing?

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

Reply to

Use Markdown with GitHub-flavored code blocks.