試圖掌握 Mocking 和測驗用例,我想測驗一個 MailableTestMail是從[email protected] 發送的,檔案提供hasTo, hasCc,hasBcc但看起來不像使用類似hasFrom. 有什么解決辦法嗎?
https://laravel.com/docs/9.x/mocking#mail-fake
public function testEmailAlwaysFrom()
{
Mail::fake();
Mail::to('[email protected]')->send(new TestMail);
Mail::assertSent(TestMail::class, function ($mail) {
return assertEquals('[email protected]', $mail->getFrom());
// return $mail->hasTo($user->email) &&
// $mail->hasCc('...') &&
// $mail->hasBcc('...');
});
}
uj5u.com熱心網友回復:
MailFake 沒有hasFrom在類中提供方法,因此將回傳 false。
但是,當使用環境變數 MAIL_FROM_ADDRESS 時,下面的解決方法不起作用,->from()必須在build().
已經報告了幾個 GitHub 問題,建議使用以下解決方法:
https://github.com/laravel/framework/issues/20056 https://github.com/laravel/framework/issues/20059
public function testEmailAlwaysFrom()
{
Mail::fake();
Mail::to('[email protected]')
->send(new TestMail);
Mail::assertSent(TestMail::class, function ($mail) {
$mail->build(); // <-- workaround
return $mail->hasTo('[email protected]') and
$mail->hasFrom('[email protected]');
});
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/468300.html
標籤:拉拉维尔 电子邮件 测试 phpunit phpmailer
上一篇:在pytest夾具中設定類屬性
下一篇:如何使用字串提示獲取用戶輸入?
