When unit testing a component, you are testing its behavior and interaction with other components. You are testing its public interface as well. If you are finding difficultly in testing your component without “ripping its chest open to the world”, this component may need to be redesigned.
I dislike the InternalsVisibleToAttribute for this reason: “if you cannot test it [through its public interface] you should not code it”.
1 comments:
Two counter-examples:
1. When publishing API to be consumed externally, it's useful to limit the surface area by making certain components internal. That doesn't mean they can't be tested nicely, it just means we don't want the public know about them.
2. Using [InternalsVisibleTo] beats the heck out of some workarounds other people use such as deep reflection or inadequate encapsulation (violating OCP). Of course the use of internals to expose what ought to be private state may still constitute a test smell; it's just somewhat less deadly than the alternatives.
Post a Comment