使用Swiftmailer 5.3.0在Symfony 2.5.5中获取此异常。我正好跟随cookbook example。调用MessageDataCollector#getMessages()
时会抛出错误:
// Check that an e-mail was sent
$this->assertEquals(1, $mailCollector->getMessageCount());
$collectedMessages = $mailCollector->getMessages();
$message = $collectedMessages[0];
消息计数断言也失败,值为零。
据我所知,收藏家未能在行动中进行任何实际收集。有什么想法吗?
答案 0 :(得分:2)
我在应用kriswallsmith's test optimization trick后遇到了同样的问题。我可以在运行开发版本时看到Web Profiler中发送邮件的结果,但无法在测试环境中获取数据。
申请Kris'技巧我注意到测试期间没有在容器中注册 swiftmailer.mailer.default.plugin.messagelogger 服务,因此的 collect()方法 MessageDataCollector类未记录邮件发送的数据。这就是为什么从swiftmailer收集器中检索信息是不可能的。
解决方案要么不覆盖 AppKernel.php 中的 initializeContainer()方法,要么覆盖它,但要确保 messagelogger 服务适用于发送邮件的测试用例。
答案 1 :(得分:1)
@codeBetyar是对的 - 当string mailto = string.Format("mailto:{0}?Subject={1}&Body={2}",
"xxx@xx.xx", Title, TextBlockTechnicalError.Text);
System.Diagnostics.Process.Start(mailto);
未在容器中注册时出现问题。
但不是摆脱优化技巧 - 你只需要为测试环境更新swiftmailer配置
swiftmailer.mailer.default.plugin.messagelogger
事情是swiftmailer:
logging: true
配置值默认等于logging
(https://github.com/symfony/swiftmailer-bundle/blob/master/DependencyInjection/Configuration.php#L121),对于第一个测试请求,kernel.debug
为true
,{ {1}} - 用于所有后续请求。
以上配置强制记录器正在注册。