samedi 18 avril 2020

How to use expectException in PHPUnit?

I'm trying to test my Exception, or any other exception in PHP Unit.

<?php declare(strict_types=1);


namespace Tests\Exception;

use PHPUnit\Framework\TestCase;

class DrinkIsInvalidExceptionTest extends TestCase
{
    public function testIsExceptionThrown(): void
    {
        $this->expectException(\Exception::class);
        try {
            throw new \Exception('Wrong exception');
        } catch(\Exception $exception) {
            echo $exception->getCode();
        }

    }

}

Still fails:

Failed asserting that exception of type "Exception" is thrown.

What could be the problem?

Aucun commentaire:

Enregistrer un commentaire