gpt4 book ai didi

PHPUnit:获取 equalTo 断言以忽略属性

转载 作者:行者123 更新时间:2023-12-01 10:29:58 24 4
gpt4 key购买 nike

我有以下 PHPUnit 测试用例:

    $mailer = $this->getMockBuilder(MailerInterface::class)->getMock();
$simpleMailer = new SimpleMailer($mailer);

$message = (new Message())
->setTo($user)
->setFrom($from)
->setSubject($subject)
->setTemplate(SimpleMailer::TEMPLATE)
->setContext(['message' => $body]);

if ($bcc) { $message->addBcc($bcc); }

$mailer
->expects($this->once())
->method('send')
->with($this->equalTo($message));

$simpleMailer->sendMessage($user, $subject, $body, $from, $bcc);

在更改 Message 类之前,这一直工作正常。 Message 类现在在构造时设置唯一 ID,这意味着 equalTo现在返回 false ,但有以下区别:
 MailerBundle\Document\Message Object (
- 'id' => '5a372f3c-a8a9-4e1e-913f-d756244c8e52'
+ 'id' => '11176427-7d74-4a3c-8708-0026ae666f8b'
'type' => null
'user' => Tests\TestUser Object (...)
'toName' => ''
'toAddress' => null
'domain' => null
'fromName' => null
'fromAddress' => 'user@example.org'
'bccAddresses' => Array (...)
'subject' => 'subject'
'textBody' => null
'htmlBody' => null
'template' => 'MailerBundle:MailTemplates:...l.twig'
'context' => Array (...)
)

有什么方法可以从相等检查中排除某些属性?

最佳答案

如果您的 Message类有 getter,你可以在 with 中使用回调函数只匹配你关心的属性。类似的东西

$mailer
->expects($this->once())
->method('send')
->with($this->callback(function(Message $message) use ($user, $from, $subject, $body) {
return $message->getTo() == $user
&& $message->getFrom() == $from
&& $message->getSubject() == $subject
&& $message->getTemplate() == SimpleMailer::TEMPLATE
&& $message->getContext()['message'] == $body
}));

关于PHPUnit:获取 equalTo 断言以忽略属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43589561/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com