W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
你可以是用 Mail
Facade 的 fake
方法來模擬郵件發(fā)送,測(cè)試時(shí)不會(huì)真的發(fā)送郵件,然后你可以斷言 mailables 發(fā)送給了用戶,甚至可以檢查他們收到的內(nèi)容。使用 fakes 時(shí),斷言一般放在測(cè)試代碼的后面:
<?php
namespace Tests\Feature;
use App\Mail\OrderShipped;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Support\Facades\Mail;
use Tests\TestCase;
class ExampleTest extends TestCase
{
public function testOrderShipping()
{
Mail::fake();
// 斷言沒有發(fā)送任何郵件...
Mail::assertNothingSent();
// 執(zhí)行訂單發(fā)送...
// 斷言已派發(fā)特定類型的郵件,以滿足給定的真實(shí)性測(cè)試...
Mail::assertSent(function (OrderShipped $mail) use ($order) {
return $mail->order->id === $order->id;
});
// 斷言一條發(fā)送給用戶的消息...
Mail::assertSent(OrderShipped::class, function ($mail) use ($user) {
return $mail->hasTo($user->email) &&
$mail->hasCc('...') &&
$mail->hasBcc('...');
});
// 斷言郵件被發(fā)送兩次...
Mail::assertSent(OrderShipped::class, 2);
// 斷言沒有發(fā)送郵件...
Mail::assertNotSent(AnotherMailable::class);
}
}
如果你用后臺(tái)任務(wù)執(zhí)行郵件發(fā)送隊(duì)列,你應(yīng)該是用 assertQueued
代替 assertSent
:
Mail::assertQueued(...);
Mail::assertNotQueued(...);
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: