我正在尝试使用带标量值的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:DoublestdClassP14]
是否有一种使用LET函数注入5的方法?
在phpspec中,与 let()
, letgo()
或 it_*()
方法的参数中的任何内容都是测试双重的。它并不是要与标量一起使用。
phpspec使用反射从类型提示或@param
注释中获取类型。然后,它用预言创建一个假对象,并将其注入方法。如果找不到类型,它将创建伪造的stdClass
。DoublestdClassP14
与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);
}
将5
铸造为 (double)
:
$this->getDiscountPrice()->shouldReturn((double)5);
或使用"比较匹配器":
$this->getDiscountPrice()->shouldBeLike('5');