0

I trying to get my hands dirty with PHPSpec.

Basically Im trying to make a TimeSpan as a practice. I want to make sure that all possible valid inputs are accepted.

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);
    }

But im receiving this error: you can not change object construction method when it is already instantiated enter image description here

Jaime Sangcap
  • 2,515
  • 6
  • 27
  • 45
  • 2
    You should try to provide errors as text in your question unless there's no other way for you to display them. – Amndeep7 Jul 28 '16 at 17:18
  • Oops yeah youre right so it will be searcheable. Thanks – Jaime Sangcap Jul 28 '16 at 17:31
  • 1
    Try writing one spec at a time. Have the examples be meaningful variations, rather than trying to validate all inputs. The goal of SpecBDD tools like phpspec is to be human readable. The issue you are seeing is due to trying to instantiate more than one of the subject-under-test within a single example. – jedifans Sep 04 '16 at 20:49
  • I guess you're right, I'll try to breakdown it to test for possible outcomes when constructing the object and see if its doing the correct behavior based on the input. – Jaime Sangcap Sep 28 '16 at 07:36

0 Answers0