mardi 13 mars 2018

Assert exception with PHPUnit

I have a function to calculate the value of one square and i want to make the test of this.

The function squaring is this:

 public function squaring($number)
    {
        if ($number == 0) {
            throw new InvalidDataException("0 can't be squared");
        }

        return $number * $number;
    }

The first step of test it's check if it's correct:

  public function testIfSquaringIsCorrect()
    {
        $number = 2;

        $result = $this->modelPractice->squaring($number);

        $this->assertEquals(4, $result);
    }

And the last step check if I get the exception.

How can I do it?

I try it like this but it's not working:

  public function testSquaringLaunchInvalidDataException()
    {
        $number = 0;

        $result = $this->modelPractice->squaring($number);

        $this->assertEquals(InvalidDataException::class,$result);

    }

Thanks!

Aucun commentaire:

Enregistrer un commentaire