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 skip a PHPUnit test?
- How to run tests in parallel with PHPUnit?
- How to use hooks in PHPUnit?
- What are PHPUnit required extensions
- How to use the PHPUnit Framework TestCase?
- How to run PHPUnit in quiet mode?
- How to show warnings in PHPUnit?
- How to load fixtures with PHPUnit?
- How to use a listener with PHPUnit?
- How to stop PHPUnit on failure?
See more codes...