let中的phpspec标量值
我尝试使用带有标量值的let函数。 我的问题是价格是双倍的,我希望是整数5let中的phpspec标量值,php,unit-testing,phpspec,Php,Unit Testing,Phpspec,我尝试使用带有标量值的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_z
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?将
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);
}
这可以用于比较,但是我在getDiscountPrice函数中乘以返回值,所以它在getDiscountPrice函数中失败,而在测试中失败。在willReturn中铸造双精度也会失败。
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);
}