скалярное значение phpspec в let

Я пытаюсь использовать функцию let со скалярными значениями. Моя проблема в том, что цена двойная, я ожидал 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);
}

Ошибка:

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

есть ли способ ввести 5 с помощью функции let?


person timg    schedule 28.01.2014    source источник


Ответы (2)


В 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);
}
person Jakub Zalas    schedule 09.02.2014

Превратить 5 в (double):

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

или используйте "сопоставитель сравнения":

$this->getDiscountPrice()->shouldBeLike('5');
person domis86    schedule 28.01.2014
comment
это сработало бы для сравнения, но я умножаю возвращаемое значение в функции getDiscountPrice, поэтому в функции getDiscountPrice произойдет сбой, а не в тесте. приведение к двойнику в willReturn также не удается. - person timg; 29.01.2014