【发布时间】: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