在 PHPSpec 中,您不能对创建的对象进行这种断言(甚至在规范文件中创建它们时对存根或模拟进行此类断言):唯一可以匹配的是 SUS (System Under Spec)及其返回值(如果有)。
我会写一个小指南来帮助你通过测试and提高您的设计和可测试性
从我的角度来看有什么问题
new
里面的用法Service
为什么是错误的
Service
有两个责任:创建一个Email
对象并完成其工作。这打破了 SRP坚实的原则。此外,您失去了对对象创建的控制,正如您所发现的,这变得非常难以测试
使规范通过的解决方法
我建议使用工厂(如下所示)来完成此类任务,因为可以显着提高可测试性,但在这种情况下,您可以通过重写规范来使测试通过,如下所示
class ServiceSpec extends ObjectBehavior
{
function it_creates_email_with_body_abc(EmailService $emailService)
{
$this->beConstructedWith($emailService);
//arrange data
$email = new Email();
$email->setBody('abc');
//assert
$emailService->send($email)->shouldBeCalled();
//act
$this->testFunc();
}
}
只要setBody
SUS 实现中没有改变,这有效。
不过我不会推荐它,因为从 PHPSpec 的角度来看这应该是一种味道。
使用工厂
创建工厂
class EmailFactory()
{
public function createEmail($body)
{
$email = new Email();
$email->setBody($body);
return $email;
}
}
及其规格
public function EmailFactorySpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType(EmailFactory::class);
}
function it_creates_email_with_body_content()
{
$body = 'abc';
$email = $this->createEmail($body);
$email->shouldBeAnInstanceOf(Email::class);
$email->getBody()->shouldBeEqualTo($body);
}
}
现在你确定createEmail
工厂的功能符合您的期望。正如你所注意到的,责任被封装在这里,你不需要在其他地方担心(考虑一个策略,你可以选择如何发送邮件:直接发送,将它们放入队列等等;如果你用原始方法处理它们,您需要在每个具体策略中测试电子邮件是否按照您的预期创建,而现在却没有)。
整合SUS工厂
class Service
{
/** @var EmailService */
protected $emailService;
/** @var EmailFactory */
protected $emailFactory;
public function __construct(
EmailService $emailService, EmailFactory $emailFactory
) {
$this->emailService = $emailService;
$this->emailFactory = $emailFactory;
}
public function testFunc()
{
$email = $this->emailFactory->createEmail('abc');
$this->emailService->send($email);
}
}
最后使规范通过(正确的方法)
function it_creates_email_with_body_abc(
EmailService $emailService, EmailFactory $emailFactory, Email $mail
) {
$this->beConstructedWith($emailService);
// if you need to be sure that body will be 'abc',
// otherwise you can use Argument::type('string') wildcard
$emailFactory->createEmail('abc')->willReturn($email);
$emailService->send($email)->shouldBeCalled();
$this->testFunc();
}
我自己没有尝试过这个例子,可能会有一些拼写错误,但我 100% 确定这种方法:我希望所有读者都清楚。