【发布时间】:2016-04-20 08:18:08
【问题描述】:
我对我的端点(api 操作)postLeadAction 进行了功能测试,在刷新新实体后,我发送电子邮件表示祝贺。我在 SwiftMailer 的帮助下使用传输 sendGrid 发送电子邮件。以及如何在发送电子邮件之前检查 sebject、fromName、fromEmail、toEmail。现在我使用 --env=test 运行测试,并在配置 swiftmailer 中为测试环境添加假脱机参数以将电子邮件文件放入目录而不是发送电子邮件
如何模拟 swiftMailer 和发送邮件前检查参数?
这是我的 config_test.yml
swiftmailer:
default_mailer: default
mailers:
default:
transport: %mailer_transport%
host: '%mailer_host%'
port: 587
encryption: ~
username: '%mailer_user%'
password: '%mailer_password%'
spool:
type: file
path: '%kernel.root_dir%/spool'
这个 MailerWrapper 类,使用 SwiftMailer 函数 'send' 发送电子邮件
class MailerWrapper
{
protected $mailer;
/**
* @var \Swift_Message
*/
//some parameters
public function __construct(\Swift_Mailer $mailer)
{
$this->mailer = $mailer;
}
public function newMessage()
{
//some parameters
return $this;
}
public function send()
{
//some logic with message
return $this->mailer->send($this->message);
}
我像食谱一样尝试
// Enable the profiler for the next request (it does nothing if the profiler is not available)
$this->client->enableProfiler();
$mailCollector = $this->client->getProfile()->getCollector('swiftmailer.mailer.default');
// Check that an email was sent
$this->assertEquals(1, $mailCollector->getMessageCount());
$collectedMessages = $mailCollector->getMessages();
$message = $collectedMessages[0];
但有错误
PHP Fatal error: Call to a member function getCollector() on a non-object
更新
在配置中我没有启用配置文件
framework:
test: ~
session:
storage_id: session.storage.mock_file
cookie_httponly: true
cookie_secure: true
profiler:
collect: false
但我有错误,因为我在 http 请求之后启用配置文件,当我之前启用时 - 一切正常
$client->enableProfiler();
$this->request(
'post',
$this->generateUrl('post_lead', [], UrlGeneratorInterface::RELATIVE_PATH),
[],
[
// some parameters
]
);
$mailCollector = $client->getProfile()->getCollector('swiftmailer');
【问题讨论】:
-
不,我在测试中启用了探查器。但是在请求之后,之前 - 一切都好。我更新问题
标签: php unit-testing symfony mocking swiftmailer