2014-01-28 22 views
4

我想用標量值使用let函數。 我的問題是,價格是一個雙,我預計一個int 5.phpspec標量值讓

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?

回答

6

在PhpSpec,不管進來的參數let()letgo()it_*()方法是測試兩倍。這並不意味着與標量一起使用。

PhpSpec使用反射從類型提示或@param註釋中獲取類型。然後它創建一個帶有預言的假對象並將其注入方法。如果它找不到類型,它將創建一個假的\stdClassDouble\stdClass\P14double類型無關。這是一個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函數中失敗,而不是在測試中。在willReturn中投入雙倍也失敗。 – timg