symfony2功能测试电子邮件数据收集器messageCount 0但发送的电子邮件

时间:2014-06-15 09:06:25

标签: symfony phpunit profiler swiftmailer

我正在尝试为注册表单实现功能测试。到目前为止,一切都有效,即使注册邮件被发送到测试电子邮件帐户。但我想查看我的代码,邮件是否被发送。

这是我到目前为止(该类扩展了WebTestCase,我正在使用Symfony2.3和phpunit 3.7.21):

$client = $this->client;
$client->followRedirects(true);
$client->enableProfiler();
$crawler = $client->request('GET', '/register');
$form = $crawler->selectButton('Register')->form();
// In here I set some form data
$client->submit($form);
$mailCollector = $client->getProfile()->getCollector('swiftmailer');
$this->assertEquals(1, $mailCollector->getMessageCount());
$collectedMessages = $mailCollector->getMessages();
$message = $collectedMessages[0];

但我总是得到Failed asserting that 0 matches expected 1.$this->assertEquals(1, $mailCollector->getMessageCount());

再一次,swiftmailer工作,通过运行测试,我得到了预期的邮件。只是Profiler似乎忽略了它。

以下是我var_dump($mailCollector);所获得的内容。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

尝试设置$client->followRedirects(false);

在后台处理POST请求和发送电子邮件后,可能会有重定向。防止重复表单提交是一种常见做法(请参阅http://en.wikipedia.org/wiki/Post/Redirect/Get)。并且测试客户端会自动执行此重定向执行另一个请求。

如果是这样,您实际上是在后续请求中尝试获取邮件收集器而不发送邮件。

相关问题