Exception based programming, where you throw an exception in an unusual case, might seem like a hard problem in unit tests. However, it isn't. Learn the process by which you can test your exceptions.
- [Instructor] Exceptions are a common way…of throwing errors to the user,…or to other parts of your application,…to then register a failure occurred,…and to then understand how to handle those particular cases.…Testing exceptions is actually a fairly easy process.…We'll do that by building an exception test…for our total method.…We'll start by first opening up our receipt test class,…in our test directory.…We'll then copy the test total and coupon method,…which lives on lines 37 through 46,…and we'll duplicate it,…on lines 48 through 57.…
We'll change the name of this method,…to instead be Test Total Exception.…In this case, we'll modify the code…to throw in the exception if the coupon value…is greater than 1.00, or, in otherwords, greater than 100%.…So we'll first modify the coupon to be 1.20, on line 50.…After this, we can delete the assert equals line,…as we won't need an assertion for the return value.…
Instead, we will assert that an exception was thrown.…So delete lines 52 through 56.…We can also remove the output on line 51,…
- Why use unit testing?
- Writing unit tests
- Extending unit tests
- Filtering PHPUnit tests
- Building dummy objects
- Working with data providers
- Writing an exception-based test
- Using TDD tactics
- Using PHPUnit advanced tactics, such as database tests
Skill Level Intermediate
Managing PHP Persistent Sessionswith David Powers2h 41m Intermediate
1. The Basics of Test-Driven Development
2. Advanced Test-Driven Development
3. Test-Driven Development Tactics
4. PHPUnit Advanced Tactics
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.