我试图使用带有标量值的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?
在PhpSpec中,无论参数是什么let()
,letgo()
或者it_*()
方法都是测试双.它并不意味着与标量一起使用.
PhpSpec使用反射从类型提示或@param
注释中获取类型.然后它创建一个带有预言的虚假对象并将其注入方法中.如果找不到类型,就会创建一个假的\stdClass
. Double\stdClass\P14
与double
类型无关.这是一个双倍的考验.
您的规格可能如下:
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); }