PHPSpec:如何在单次测试中多次调用工厂方法

时间:2016-07-28 15:44:49

标签: unit-testing tdd phpspec

我试图用PHPSpec弄脏手。

基本上我试图将TimeSpan作为一种练习。 我想确保接受所有可能的有效输入。

function it_can_be_created_from_24_format()
    {
        $hours = 24;
        $minutes = 60;
        $seconds = 60;
        for ($h = 1; $h < $hours; $h++) {
            for ($m = 1; $m < $minutes; $m++) {
                for ($s = 1; $s < $seconds; $s++) {
                    $this->beConstructedThrough('fromString', [$this->buildTimeString($h, $m, $s)]);
                    $this->shouldNotThrow(\InvalidArgumentException::class)->duringInstantiation();
                    $this->getHours()->shouldBe($h);
                    $this->getMinutes()->shouldBe($m);
                    $this->getSeconds()->shouldBe($s);
                }
            }
        }
    }


private function buildTimeString($h, $m, $s)
    {
        $minutes = ($m < 9) ? '0'.$m : $m;
        $seconds = ($s < 9) ? '0'.$s : $s;
        return sprintf('%s:%s:%s', $h, $minutes, $seconds);
    }

但我收到此错误: you can not change object construction method when it is already instantiated enter image description here

0 个答案:

没有答案