The scalar value of phpspec in let

I am trying to use the let function with scalar values. My problem is that the price is Double, I was expecting int 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);
}

mistake:

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

is there any way to inject 5 using let function?

+4
source share
2 answers

In PhpSpec, everything that is included in the argument let(), letgo()or it_*(), is a test double. It is not intended for use with scalars.

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 important for the example
    $this->beConstructedWith($buyable, 0);

    $buyable->getPrice()->willReturn(5);

    $this->getDiscountPrice()->shouldReturn(5);
}
+6

5 (double):

$this->getDiscountPrice()->shouldReturn((double)5);

:

$this->getDiscountPrice()->shouldBeLike('5');
-2

Source: https://habr.com/ru/post/1523982/


All Articles