COR[R]ECT: [R]eference
When testing a method, consider:
-
What it references outside its scope
-
What external dependencies it has
-
Whether it depends on the object being in a certain state
-
Any other conditions that must exist
A web app that displays a customer’s account history might require the customer to be logged on. The pop() method for a stack requires a nonempty stack. Shifting your car’s transmission from Drive to Park requires you to first stop—if your transmission allowed the shift while the car was moving, it’d likely deliver some hefty damage to your fine Geo Metro.
When you make assumptions about any state, you should verify that your code is reasonably well-behaved when those assumptions are not met. Imagine you’re developing the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access