W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
你可以使用 Notification
Facade 的 fake
方法來模擬通知的發(fā)送,測試時并不會真的發(fā)出通知。然后你可以斷言 notifications 發(fā)送給了用戶,甚至可以檢查他們收到的內(nèi)容。使用 fakes 時,斷言一般放在測試代碼后面:
<?php
namespace Tests\Feature;
use App\Notifications\OrderShipped;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Notifications\AnonymousNotifiable;
use Illuminate\Support\Facades\Notification;
use Tests\TestCase;
class ExampleTest extends TestCase
{
public function testOrderShipping()
{
Notification::fake();
// 斷言沒有發(fā)送通知...
Notification::assertNothingSent();
// 執(zhí)行訂單發(fā)送...
// 斷言已發(fā)送特定類型的通知以滿足給定的真實性測試...
Notification::assertSentTo(
$user,
function (OrderShipped $notification, $channels) use ($order) {
return $notification->order->id === $order->id;
}
);
// 斷言向給定用戶發(fā)送了通知...
Notification::assertSentTo(
[$user], OrderShipped::class
);
// 斷言沒有發(fā)送通知...
Notification::assertNotSentTo(
[$user], AnotherNotification::class
);
// 斷言通過 Notification::route() 方法發(fā)送通知...
Notification::assertSentTo(
new AnonymousNotifiable, OrderShipped::class
);
// 斷言通過 Notification :: route() 方法向給定用戶發(fā)送了通知...
Notification::assertSentTo(
new AnonymousNotifiable,
OrderShipped::class,
function ($notification, $channels, $notifiable) use ($user) {
return $notifiable->routes['mail'] === $user->email;
}
);
}
}
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: