4

スカラー値で let 関数を使用しようとしています。私の問題は、価格が Double であることです。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]

let 関数を使用して 5 を注入する方法はありますか?

4

2 に答える 2

6

PhpSpec ではlet()letgo()it_*()メソッドの引数に含まれるものは何でも test double です。スカラーで使用するためのものではありません。

PhpSpec はリフレクションを使用して、型ヒントまたは@param注釈から型を取得します。次に、予言を含む偽のオブジェクトを作成し、それをメソッドに注入します。型が見つからない場合は、 の偽物を作成します\stdClassDouble\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);
}
于 2014-02-09T22:28:50.367 に答える