【问题标题】:Asserting email was sent with mailhog dsn断言电子邮件是使用 mailhog dsn 发送的
【发布时间】:2022-08-16 13:33:24
【问题描述】:

测试我的电子邮件发送逻辑,不会捕获发送到 mailhog 的电子邮件。

// Both ways don\'t work
$this->assertEmailCount(1, \'smtp://mailhog:1025\');
$this->assertEmailCount(1);

我得到的两个错误:

断言传输 smtp://mailhog:1025 已发送“1”电子邮件(已发送 0)失败。

断言传输已发送“1”电子邮件(已发送 0)失败。

电子邮件已发送。我在mailhog中看到它。每次我运行测试。

private function getMailer(array $mailConfig): MailerInterface
{
    $dsn = match (strtolower($mailConfig[\'transport\'])) {
        \'sendgrid\' => \'smtp://mailhog:1025\',
        \'mailchimp\' => \'smtp://mailhog:1025\',
        default => \'smtp://mailhog:1025\'
    };

    $transport = Transport::fromDsn($dsn);

    return new Mailer($transport);
}

也发布发送功能,但认为何时发送电子邮件并不重要。用 mailhog 测试时有什么特别的吗?

  • 你检查过不同的环境配置吗?

标签: symfony phpunit integration-testing mailhog


【解决方案1】:

我遇到了同样的问题,我只是添加

//...
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Mailer\Mailer;

public function __construct(
        //...
        private readonly EventDispatcherInterface $eventDispatcher
) {
}

public function getMailer(): Mailer
{
    //...
    $transport = Transport::fromDsn($dsn, $this->eventDispatcher);
    return new Mailer($transport);
}

您还可以在测试中禁用交付https://symfony.com/doc/current/mailer.html#disabling-delivery

【讨论】:

    猜你喜欢
    • 2017-10-06
    • 2023-02-15
    • 1970-01-01
    • 2016-04-17
    • 2017-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多