[英]How to test a queued notification?
我有一个实现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
中注册的 function 中:
protected function registered(Request $request, $user)
{
$user->notify(new WelcomeNotification());
// other code
}
在使用Notification::assertSentTo
注册时,我已成功断言WelcomeNotification
已正确发送给用户。
我的问题是如何测试WelcomeNotification
是否被推送到队列中?
您需要配置队列驱动程序(数据库将是最简单的),然后运行php artisan queue:work
。 查看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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.