-1

I'm trying to spec value object with static constructor in phpspec and the problem it says that shouldBeLike is undefined method. The object returned from times method is new Money, so I don't know what is wrong...

MoneySpec:

<?php

namespace spec;

use Money;
use PhpSpec\ObjectBehavior;

class MoneySpec extends ObjectBehavior
{
    function it_is_initializable()
    {
        $this->shouldHaveType(Money::class);
    }

    function it_multiplies()
    {
        $five = Money::dollar(5);
        $five->times(2)->shouldBeLike(Money::dollar(5));
        $five->times(3)->shouldBeLike(Money::dollar(6));
    }
}

Money:

<?php

class Money
{
    private $amount;

    private function __construct($amount)
    {
        $this->amount = $amount;
    }

    public static function dollar($amount)
    {
        return new Money($amount);
    }

    public function times($multiplier)
    {
        return new Money($this->amount * $multiplier);
    }

    public function getAmount()
    {
        return $this->amount;
    }
}

2 Answers 2

1

For named static constructors you need use beConstructedThrough (or one of the shorter syntax described). For example, with your case:

function it_multiplies()
{
    $this->beConstructedThrough('dollar', [5]);
    $this->times(2)->shouldBeLike(Money::dollar(5));
    $this->times(3)->shouldBeLike(Money::dollar(6));
}
0

Because You didn't define it... I can't see it in the code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.