【问题标题】:PHPUnit Testing the Send Method parameters from SwiftMailPHPUnit 测试来自 SwiftMail 的发送方法参数
【发布时间】:2012-10-13 21:46:32
【问题描述】:

也许我把这个复杂化了,但是我有一个被另一个类调用的电子邮件对象,而电子邮件类使用了一个 Swift Mailer 的实例。

$email = Email::instance();
$mailer = $this->getMock('Swift_Mailer', array('send'), array(new \Swift_NullTransport()));
$email->setTransport($mailer);
$mailer->expects($this->once())
    ->method('send');
$model->sendEmail('user_email@test.com');

如上所述,我可以轻松测试是否调用了 send 方法并正确确认正在发送电子邮件,但是,我需要测试与邮件一起发送的 Swift Mailer Message 的主题。

$email = Email::instance();
$mailer = $this->getMock('Swift_Mailer', array('send'), array(new \Swift_NullTransport()));
$email->setTransport($mailer);
$mailer->expects($this->once())
    ->method('send')
    ->with($this->equalTo('new email subject'));

显然这不起作用,并且抛出了很多错误。

关于如何测试这个有什么想法吗?

【问题讨论】:

  • 另一种方法是将电子邮件的发送逻辑放入一个方法中,为此方法编写一个功能测试,当需要发送电子邮件时,您可以模拟此方法。

标签: php phpunit swiftmailer


【解决方案1】:

找到了解决方法。

我没有测试 Swift Mail 内容,而是模拟模型并测试传递给调用电子邮件对象类的函数的参数。

绝对不漂亮,但它会解决上面的具体问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-06
    • 2018-08-05
    • 1970-01-01
    • 2021-12-04
    • 2013-04-05
    • 2021-10-19
    • 1970-01-01
    • 2013-11-08
    相关资源
    最近更新 更多