Unit testing factory methods which have a concrete class as a return type

Often, there’s nothing wrong with creating public properties that can be used for state-based testing. Yes: It’s code you created to enable a test scenario, but does it hurt your API? Is it conceivable that other clients would find the same property useful later on?

There’s a fine line between test-specific code and Test-Driven Design. We shouldn’t introduce code that has no other potential than to satisfy a testing requirement, but it’s quite alright to introduce new code that follow generally accepted design principles. We let the testing drive our design – that’s why we call it TDD 🙂

Adding one or more properties to a class to give the user a better possibility of inspecting that class is, in my opinion, often a reasonable thing to do, so I don’t think you should dismiss introducing such properties.

Apart from that, I second nader’s answer 🙂

Leave a Comment