最合适的回答,由SO网友:J.D. 整理而成
我正在为涉及的代码进行一些测试wp_mail() 就在前几天。我也找不到任何文档,但我看了一下the source of mock-mailer.php, 一切都很简单。
首先,您应该了解WordPress总是使用$phpmailer 用于保存的实例的全局PHPMailer 使用的对象wp_mail(). 在测试期间$phpmailer 全局自动set to be an instance of MockPHPMailer 相反
这个MockPHPMailer 实际上并不发送任何邮件,它只是收集有关“发送”的每条邮件的信息。要检索此信息,可以调用对象的get_sent() 方法要检索对象本身,如下所示tests_retrieve_phpmailer_instance() 旨在使用。
因此,在一个简单的测试中,您只想检查是否没有发送邮件,您可以这样做:
$this->assertEmpty( tests_retrieve_phpmailer_instance()->get_sent() );
当然,您也可以执行更复杂的检查,如以下所示(我自己测试中的一个真实示例):
$email = tests_retrieve_phpmailer_instance()->get_sent();
$this->assertEquals( array( array( \'test@example.com\', \'\' ) ), $email->to );
$this->assertStringMatchesFormat( \'%sItem 03%s\', $email->body );
$this->assertStringMatchesFormat( \'%sA Donor%s\', $email->body );
$this->assertStringMatchesFormat(
\'%s\' . get_the_title( $wish_list_id ) . \'%s\'
, $email->body
);
返回的电子邮件对象
get_sent() 有
the following properties: