如何测试从Laravel中的Notification发送的邮件

问题描述 投票:0回答:2

我的用户可以指定他们是否立即向他们发送通知,或者每天通知任何待处理的通知。

以下工作正常,手动测试;现在我想写一个功能测试,以确保发送电子邮件,而不是短信或什么都没有。

public function test_instant_notification_is_sent(): void
{
    Mail::fake();

    $this->user = $user = factory(User::class)->create();

    $this->user->update(
        [
            'notification_frequency' => 'instant',
            'notification_method' => 'email',
            'notification_email' => $this->email,
        ]
    );

    $this->user->save();

    $email = ['subject' => 'subject', 'data' => 'data'];
    $sms = 'Test Notification';
    $database = ['some' => 'data'];

    $this->user->notify(new TestNotification($email, $sms, $database));

    Mail::assertSent(MailMessage::class);
}

我写了一个TestNotification来配合它。

class TestNotification extends Notification
{
    use Queueable;

    public function __construct(array $email, string $sms, array $database)
    {
        $this->email = $email;
        $this->sms = $sms;
        $this->database = $database;
    }

    public function via($notifiable): array
    {
        return [$notifiable->preferredNotificationMethod()];
    }

    public function toDatabase($notifiable): array
    {
        return $this->database;
    }

    public function toNexmo($notifiable): array
    {
        return (new NexmoMessage)
            ->content($this->sms);
    }

    public function toMail($notifiable): MailMessage
    {
        return (new MailMessage)
            ->greeting($this->email['subject'])
            ->line('Testing Notifications!')
            ->line('Thank you for using our application!');
    }
}

我的测试失败了,因为我现在看到,MailMessage不是Mailable,我认为Mail::fake()和邮件断言只适用于Mailable。顺便说一句,如果我删除Mail::fake()电子邮件是好的。

如果没有实际发送电子邮件,其他人如何设法测试这个?

laravel testing mocking
2个回答
1
投票

在测试通知时,您将需要使用Notification::fake()并进行这些断言。请参阅文档的Notification Fake以了解我的意思。

    public function test_instant_notification_is_sent(): void
    {
        Notification::fake();

        $this->user = $user = factory(User::class)->create();

        $this->user->update(
            [
                'notification_frequency' => 'instant',
                'notification_method' => 'email',
                'notification_email' => $this->email,
            ]
        );

        $this->user->save();

        $email = ['subject' => 'subject', 'data' => 'data'];
        $sms = 'Test Notification';
        $database = ['some' => 'data'];

        $this->user->notify(new TestNotification($email, $sms, $database));

        Notification::assertSentTo($this->user, TestNotification::class, function ($notification, $channels) {
            // check channels or properties of $notification here
            return in_array('mail', $channels);

            // or maybe check for nexmo:
            return in_array('nexmo', $channels);
        });
    }

1
投票

答案结果是检查via方法,因为这只设置为一个通道(在我的情况下)。而@FatBoyXPC指出,我根本不需要查看发送的电子邮件。最终解决方案

       Notification::fake();

        $this->user->update(
            [
                'notification_frequency' => 'instant',
                'notification_method' => 'email',
                'notification_email' => $this->email,
            ]
        );
        $email = ['subject' => 'Test Subject', 'data' => 'data'];
        $sms = 'Test Notification';
        $database = ['some' => 'data'];

        $notification = new TestNotification($email, $sms, $database);

        $this->user->notify($notification);

        $this->assertEquals(['mail'], $notification->via($this->user));

现在,如果我改变emailinstant这个测试失败并通过emailinstant

© www.soinside.com 2019 - 2024. All rights reserved.