phpunitHow to use expects twice in PHPUnit?
Using expects
twice in PHPUnit is possible by using the expectException
and expectExceptionMessage
methods.
Example code
public function testException()
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('Exception message');
throw new Exception('Exception message');
}
Output example
OK (1 test, 1 assertion)
Code explanation
$this->expectException(Exception::class);
- This line sets the expectation that an exception of typeException
will be thrown.$this->expectExceptionMessage('Exception message');
- This line sets the expectation that the exception thrown will have the messageException message
.throw new Exception('Exception message');
- This line throws an exception of typeException
with the messageException message
.
Helpful links
More of Phpunit
- How to show warnings in PHPUnit?
- How to stop PHPUnit on failure?
- How to skip a PHPUnit test?
- How to use the PHPUnit Framework TestCase?
- How to ignore a test in PHPUnit?
- How to use getMockBuilder with PHPUnit?
- How to use dependency injection in PHPUnit?
- How to generate a coverage report with PHPUnit?
- How to run all PHPUnit tests?
- How to install PHPUnit with a PHAR file?
See more codes...