pl中的phpspec标量值

 葉の鋼琴曲 发布于 2023-01-30 23:32

我试图使用带有标量值的let函数.我的问题是价格是双倍的,我预计是5.

function let(Buyable $buyable, $price, $discount)
{
    $buyable->getPrice()->willReturn($price);
    $this->beConstructedWith($buyable, $discount);
}

function it_returns_the_same_price_if_discount_is_zero($price = 5, $discount = 0) {
    $this->getDiscountPrice()->shouldReturn(5);
}

错误:

? it returns the same price if discount is zero
expected [integer:5], but got [obj:Double\stdClass\P14]

有没有办法使用let函数注入5?

1 个回答
  • 在PhpSpec中,无论参数是什么let(),letgo()或者it_*()方法都是测试双.它并不意味着与标量一起使用.

    PhpSpec使用反射从类型提示或@param注释中获取类型.然后它创建一个带有预言的虚假对象并将其注入方法中.如果找不到类型,就会创建一个假的\stdClass. Double\stdClass\P14double类型无关.这是一个双倍的考验.

    您的规格可能如下:

    private $price = 5;
    
    function let(Buyable $buyable)
    {
        $buyable->getPrice()->willReturn($this->price);
    
        $this->beConstructedWith($buyable, 0);
    }
    
    function it_returns_the_same_price_if_discount_is_zero() 
    {
        $this->getDiscountPrice()->shouldReturn($this->price);
    }
    

    虽然我更愿意包含与当前示例相关的所有内容:

    function let(Buyable $buyable)
    {
        // default construction, for examples that don't care how the object is created
        $this->beConstructedWith($buyable, 0);
    }
    
    function it_returns_the_same_price_if_discount_is_zero(Buyable $buyable) 
    {
        // this is repeated to indicate it's important for the example
        $this->beConstructedWith($buyable, 0);
    
        $buyable->getPrice()->willReturn(5);
    
        $this->getDiscountPrice()->shouldReturn(5);
    }
    

    2023-01-30 23:40 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有