2014-01-28 3 views
4

Я пытаюсь использовать функцию let со скалярными значениями. Моя проблема заключается в том, что цена является двойной, я ожидал ИНТ 5.phpspec скалярное значение в let

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?

ответ

6

В PhpSpec, что приходит в качестве аргумента let(), letgo() или it_*() методов является двойной тест. Он не предназначен для использования со скалярами.

PhpSpec использует отражение, чтобы получить тип от типа подсказки или аннотации @param. Затем он создает поддельный объект с пророчеством и вводит его в метод. Если он не может найти тип, он создаст фальшивку \stdClass. Double\stdClass\P14 не имеет отношения к double. Это test 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); 
} 
-2

В ролях 5 к (double):

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

или использовать "comparison matcher":

$this->getDiscountPrice()->shouldBeLike('5'); 
+0

это будет работать для сравнения, но я умножения возвращаемого значения в функции getDiscountPrice, поэтому он будет терпеть неудачу в функции getDiscountPrice, а не в тесте. литье в double в willReturn также терпит неудачу. – timg