如何测试排队的通知?

我有一个WelcomeNotification实现了ShouldQueue

class WelcomeNotification extends Notification implements ShouldQueue
{
    use Queueable;

    public function via($notifiable)
    {
        return ['mail'];
    }

    public function toMail($notifiable)
    {
        // send a welcome message to the user via mailtrap
    }

}

如何测试此通知是否已推送到队列?

我正在尝试像这样测试它,但是我正在 The expected [App\Notifications\WelcomeNotification] job was not pushed

    /** @test */
    public function welcome_notification_is_pushed_to_queue()
    {
        Queue::fake();

        $this->post(route('register'),[
            'name' => 'John Doe','email' => 'john@test.com','password' => 'passwordtest',]);

        Queue::assertPushed(WelcomeNotification::class);
    }

在我在RegisterController中注册的功能内:

    protected function registered(Request $request,$user)
    {
          $user->notify(new WelcomeNotification());
          // other code
    }

我已经成功断言了使用WelcomeNotification注册时已正确地将Notification::assertSentTo发送给用户。

我的问题是如何测试WelcomeNotification是否被推入队列?

jiajia2xm 回答:如何测试排队的通知?

您需要配置队列驱动程序(最简单的数据库),然后运行php artisan queue:work。签出docs at laravel.com

,

如果在伪造队列后转储 Queue::pushedJobs()。您会发现所有排队的作业都是一个数组,其中数组键是作业名称。实际上,Illuminate\Notifications\SendQueuedNotifications 是在您的通知排队时分派的。

/** @test */
public function welcome_notification_is_pushed_to_queue()
{
    Queue::fake();

    $this->post(route('register'),[
        'name' => 'John Doe','email' => 'john@test.com','password' => 'passwordtest',]);
    
    // dd(Queue::pushedJobs());

    Queue::assertPushed(\Illuminate\Notifications\SendQueuedNotifications::class,1);
}
本文链接:https://www.f2er.com/3167183.html

大家都在问