Laravel testing on demand notification

拜拜、爱过 提交于 2019-12-13 14:45:56

问题


I have a slack notification class that sends a message inside our company slack account, 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 notifications!

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 test 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?


回答1:


For the newcomers

Laravel introduces in v5.5.14 the ability to test anonymous notification by using the provided Notification::fake() system.

More about this new feature here: https://github.com/laravel/framework/pull/21379



来源:https://stackoverflow.com/questions/46277323/laravel-testing-on-demand-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!