This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Sometimes code throws an exception when not used properly. PHPUnit gives us way to test for that output as well.
NOTE
In namespaced context the Exception class needs to be prefixed with global prefix operator. If you get a failure message when trying to use an InvalidArgumentException, add a slash before the exception to make sure you are using the exception from the global namespace: \InvalidArgumentException
Testing Exceptions
PHPUnit Exception Documentation
Tips
- Excepted exceptions are written BEFORE the code expected to throw the exception.
- If you're having problems with
InvalidArgumentException
you can try calling the base implementation of that exception by using the global namespace (a \ before the exception).\InvalidArgumentException
Example:
/** @test */
function addIngredientMustReceiveValidAmount()
{
$this->expectException(\InvalidArgumentException::class);
$recipe = new Recipe();
$recipe->addIngredient("garlic", "two");
}
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up