我有一个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
是否被推入队列?
答案 0 :(得分:0)
您需要配置队列驱动程序(最简单的数据库),然后运行php artisan queue:work
。签出docs at laravel.com。
答案 1 :(得分:0)
如果在伪造队列后转储 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);
}