Test Driven Validation Logic with Extract & Override
Historical article · Originally published May 16, 2012; technical details may be outdated.
In the NerdDinner project, there’s this amazing test (see the bottom, second one).
There are so many things wrong here:
- What is exactly correct about these properties?
- Many magic values
- Multiple tests in a single test
- if it fails, its hard to find out why
Here’s how I would approach this kind of validation logic:
use Extract and override, with the test setting the flags for all ‘other’ validation functions except the one I’m testing right now. and I’d at least have two tests for each validation logic. This can also be accomplished with multiple classes and an interface, but I find the tests this way are more readable.
Lessons on this topic
- Extract And Override With Validation Logic · Legacy Code Hero
- Extract And Override Refactoring · The Art of Unit Testing and TDD in Java
- Testing Complex Validation Knowledge using Extract & Override · The Art of Unit Testing & TDD Master Class in .NET
- Best Practices Continued · TDD in .NET in Hebrew
- Using Extract & Override · The Art of Unit Testing & TDD Master Class in .NET