Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从存根函数参数获取属性?_Php_Testing_Phpspec - Fatal编程技术网

Php 如何从存根函数参数获取属性?

Php 如何从存根函数参数获取属性?,php,testing,phpspec,Php,Testing,Phpspec,我有一个服务,它应该创建一个email类对象并将其传递给第三个类(email sender) 我想检查电子邮件的正文,它是由函数生成的 Service.php class Service { /** @var EmailService */ protected $emailService; public function __construct(EmailService $emailService) { $this->emailServic

我有一个服务,它应该创建一个email类对象并将其传递给第三个类(email sender)

我想检查电子邮件的正文,它是由函数生成的

Service.php

class Service
{
    /** @var EmailService */
    protected $emailService;

    public function __construct(EmailService $emailService)
    {
        $this->emailService = $emailService;
    }

    public function testFunc()
    {
        $email = new Email();
        $email->setBody('abc'); // I want to test this attribute

        $this->emailService->send($email);
    }
}
interface EmailService
{
    public function send(Email $email);
}
Email.php:

class Email
{
    protected $body;

    public function setBody($body)
    {
        $this->body = $body;
    }
    public function getBody()
    {
        return $this->body;
    }
}
EmailService.php

class Service
{
    /** @var EmailService */
    protected $emailService;

    public function __construct(EmailService $emailService)
    {
        $this->emailService = $emailService;
    }

    public function testFunc()
    {
        $email = new Email();
        $email->setBody('abc'); // I want to test this attribute

        $this->emailService->send($email);
    }
}
interface EmailService
{
    public function send(Email $email);
}
所以我为emailService和email创建了一个存根类。但我无法验证电子邮件的正文。我也无法检查是否调用了$email->setBody(),因为email是在测试函数中创建的

class ServiceSpec extends ObjectBehavior
{
    function it_creates_email_with_body_abc(EmailService $emailService, Email $email)
    {
        $this->beConstructedWith($emailService);

        $emailService->send($email);
        $email->getBody()->shouldBe('abc');
        $this->testFunc();
    }
}
我明白了:

Call to undefined method Prophecy\Prophecy\MethodProphecy::shouldBe() in /private/tmp/phpspec/spec/App/ServiceSpec.php on line 18 

在真实的应用程序中,主体是生成的,所以我想测试它是否正确生成。我该怎么做呢?

在PHPSpec中,您不能对创建的对象(甚至在spec文件中创建它们时对存根或模拟对象)进行这种断言:唯一可以匹配的是SUS(SsystemUnderSpec)及其返回值(如果有的话)

我将编写一个小指南,使您的测试通过以改进您的设计和可测试性


在我看来有什么不对
内部使用
服务

为什么是错的
服务
有两项职责:创建
电子邮件
对象并完成其工作。这是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();
    }
}
只要在SUS实现中
setBody
不发生变化,这就可以工作。 然而,我不推荐它,因为从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中 最终通过规范(正确的方法)
我没有尝试过这个例子,可能会有一些打字错误,但我100%确信这种方法:我希望对所有读者都清楚。

谢谢。我真的很感激!我在这里上传了工作代码: