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 typeExceptionwill 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 typeExceptionwith the messageException message.
Helpful links
More of Phpunit
- How to show warnings in PHPUnit?
- How to stop PHPUnit on failure?
- How to run tests in parallel with PHPUnit?
- How to skip a PHPUnit test?
- How to mock a static method with PHPUnit?
- How to mock a method with different arguments in PHPUnit?
- How to generate a JUnit report in PHPUnit?
- How to run all PHPUnit tests?
- How to mock a method with PHPUnit?
- What are PHPUnit required extensions
See more codes...