Yii2 BaseMailer测试环境未保存或发送

Yii2 BaseMailer测试环境未保存或发送,yii2,swiftmailer,codeception,Yii2,Swiftmailer,Codeception,我正在运行一些单元测试,其中一个测试检查电子邮件的生成和发送 我已经检查了文档,为了强制Yii将电子邮件保存到一个文件中而不是发送,我配置了mailer组件,如下所示: 'mailer' => [ 'class' => 'yii\swiftmailer\Mailer', 'viewPath' => '@common/mail', // send all mails to a file by default. You have to

我正在运行一些单元测试,其中一个测试检查电子邮件的生成和发送

我已经检查了文档,为了强制Yii将电子邮件保存到一个文件中而不是发送,我配置了
mailer
组件,如下所示:

'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'viewPath' => '@common/mail',
        // send all mails to a file by default. You have to set
        // 'useFileTransport' to false and configure a transport
        // for the mailer to send real emails.
        'useFileTransport' => true
    ],
运行测试时,我看到来自
BaseMailer send()
函数的
Yii::info
消息

[yii\mail\BaseMailer::send]'发送电子邮件“直接借记付款通知”至“test@test.co.uk“'

但是,电子邮件不会保存在任何地方,应该是
运行时/mail
,也不会发送到任何地方

我尝试在运行时使用以下方法设置
useFileTransport

$mailer = Yii::$app->mailer;
$mailer->useFileTransport = true;
$mailer->composer... 

但是没有任何改变,任何帮助都是值得感激的

查看文档,
yii\mail\BaseMailer::useFileTransport
如果启用,此选项将强制将邮件数据保存到本地文件中,而不是定期发送。这些文件将保存在
yii\mail\BaseMailer::fileTransportPath
下,默认情况下为
@runtime/mail

因此,请检查您的目录权限,如果它们正常,您可以使用
fileTransportPath
将其更改为您知道不会有权限问题的任何其他目录

'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'viewPath' => '@common/mail',
        // send all mails to a file by default. You have to set
        // 'useFileTransport' to false and configure a transport
        // for the mailer to send real emails.
        'useFileTransport' => true,
        'fileTransportPath'=>'your/path',
    ],

查看文档,
yii\mail\BaseMailer::useFileTransport
如果启用,此选项将强制将邮件数据保存到本地文件中,而不是定期发送。这些文件将保存在
yii\mail\BaseMailer::fileTransportPath
下,默认情况下为
@runtime/mail

因此,请检查您的目录权限,如果它们正常,您可以使用
fileTransportPath
将其更改为您知道不会有权限问题的任何其他目录

'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'viewPath' => '@common/mail',
        // send all mails to a file by default. You have to set
        // 'useFileTransport' to false and configure a transport
        // for the mailer to send real emails.
        'useFileTransport' => true,
        'fileTransportPath'=>'your/path',
    ],

Codeception覆盖要使用的邮件程序设置(),该设置不发送或保存任何内容。这是有道理的-你不想在测试期间发送一大堆电子邮件

您可以使用Codeception自定义断言或方法来测试发送的电子邮件:

$I->seeEmailIsSent(3); // Test that three emails was sent

$messages = $I->grabSentEmails();
$I->assertEquals('admin@site,com', $messages[0]->getTo()); 

Codeception覆盖要使用的邮件程序设置(),该设置不发送或保存任何内容。这是有道理的-你不想在测试期间发送一大堆电子邮件

您可以使用Codeception自定义断言或方法来测试发送的电子邮件:

$I->seeEmailIsSent(3); // Test that three emails was sent

$messages = $I->grabSentEmails();
$I->assertEquals('admin@site,com', $messages[0]->getTo()); 

你是说
@runtime/mail
没有保存电子邮件吗?它是设置了
useFileTransport
时保存电子邮件的默认路径。您对该目录有足够的权限吗?您是说
@runtime/mail
没有保存电子邮件吗?它是设置了
useFileTransport
时保存电子邮件的默认路径。你对这个目录有足够的权限吗?是的,这是有道理的,他们不应该发送。只需保存它们,然后我就可以做一个断言来检查生成的电子邮件的正确数量。是的,这是有道理的,它们从来都不应该发送。只需保存它们,然后我就可以做一个断言来检查生成的电子邮件的正确数量