Test Driven Validation Logic with Extract & Override

By Roy Osherove In Unit Testing

Markdown version

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

More in Unit Testing

All posts in Unit Testing