-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;
    }
}
George Kagan
  • 5,913
  • 8
  • 46
  • 50
Karol F
  • 1,556
  • 2
  • 18
  • 33

2 Answers2

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));
}
nj_
  • 2,219
  • 1
  • 10
  • 12
0

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

djxyz
  • 125
  • 4