如何测试邮件是否已从通知中发送?
How to test that a mailable was sent from a notification?
测试从通知发送邮件的秘诀是什么?
正在测试通知:
it('does send notification when model is deleted', function() {
Notification::fake();
$this->model->delete();
Notification::assertSentTo($this->model->user, MyModelDeletedNotification::class);
});
通过。
测试可邮寄:
it('does send email when model is deleted', function() {
Mail::fake();
$this->model->delete();
Mail::assertQueued(MyModelDeletedMail::class, 1);
});
失败。没有邮件排队。
删除模型时会触发观察者方法:
public function deleted(MyModel $model)
{
if ($model->isForceDeleting()) {
return;
}
$model->user->notify(new MyModelDeletedNotification($model));
}
通知:
class MyModelDeleted extends Notification implements ShouldQueue
{
use Queueable;
...
public function via($notifiable)
{
return ['mail', 'database'];
}
public function toMail($notifiable)
{
return (new MyModelDeletedMail($this->model))->to($notifiable->email);
}
...
}
可邮寄:
class ConsultationDeleted extends Mailable
{
use Queueable, SerializesModels;
...
public function build()
{
...
}
}
当我在邮件构造函数或构建方法中转储(“foobar”)时,消息出现在日志中。但是测试失败。我在这里错过了什么?
关于在通知上下文中使用 Mail:fake:
It is not a very good way to test because it catches only mails sent
using the Mail facade (doesn't intercept mails sent through a
notification or using a mailer retrieved via dependency injection).
确保您已设置
<server name="MAIL_MAILER" value="array"/>
phpunit.xml。在你的测试中:
it('does send email', function() {
// business logic
$emails = app()->make('mailer')->getSwiftMailer()->getTransport()->messages();
assertCount(1, $emails);
assertEquals([$this->user->email], array_keys($emails[0]->getTo()));
});
这对我有用laravel 8。
测试从通知发送邮件的秘诀是什么?
正在测试通知:
it('does send notification when model is deleted', function() {
Notification::fake();
$this->model->delete();
Notification::assertSentTo($this->model->user, MyModelDeletedNotification::class);
});
通过。
测试可邮寄:
it('does send email when model is deleted', function() {
Mail::fake();
$this->model->delete();
Mail::assertQueued(MyModelDeletedMail::class, 1);
});
失败。没有邮件排队。
删除模型时会触发观察者方法:
public function deleted(MyModel $model)
{
if ($model->isForceDeleting()) {
return;
}
$model->user->notify(new MyModelDeletedNotification($model));
}
通知:
class MyModelDeleted extends Notification implements ShouldQueue
{
use Queueable;
...
public function via($notifiable)
{
return ['mail', 'database'];
}
public function toMail($notifiable)
{
return (new MyModelDeletedMail($this->model))->to($notifiable->email);
}
...
}
可邮寄:
class ConsultationDeleted extends Mailable
{
use Queueable, SerializesModels;
...
public function build()
{
...
}
}
当我在邮件构造函数或构建方法中转储(“foobar”)时,消息出现在日志中。但是测试失败。我在这里错过了什么?
关于在通知上下文中使用 Mail:fake:
It is not a very good way to test because it catches only mails sent using the Mail facade (doesn't intercept mails sent through a notification or using a mailer retrieved via dependency injection).
确保您已设置
<server name="MAIL_MAILER" value="array"/>
phpunit.xml。在你的测试中:
it('does send email', function() {
// business logic
$emails = app()->make('mailer')->getSwiftMailer()->getTransport()->messages();
assertCount(1, $emails);
assertEquals([$this->user->email], array_keys($emails[0]->getTo()));
});
这对我有用laravel 8。