How do I test for an exact Exception message, rather than a substring, with PHPUnit?

后端 未结 6 1717
难免孤独
难免孤独 2021-01-12 07:14

According to the PHPUnit Documentation on @expectedExceptionMessage, the string must only be a substring of the actual Exception thrown.

In

6条回答
  •  囚心锁ツ
    2021-01-12 07:30

    Another possible solution, to make sure that error have happened:

            $errorHappened = false;
            try {
                //call your code here 
            } catch (\Exception $e) {
                $errorHappened = true;
                $this->assertEquals($e->getMessage(), "Expected error text");
                $this->assertEquals($e->getCode(), "Expected error code");
            }
            $this->assertTrue($errorHappened);
    

提交回复
热议问题