Recently I have taken to unit testing my code before I write it,this doesn’t exactly speed up the development process, but it does mean the code compiles for a much greater amount of time.
One little issue I have come across is whether or not I should be testing internal classes. I can test the public interface on a public class with no issues, but if I have an internal component with which a public class is to interact, I feel compelled to test the interface of that class to make sure it does what it should be doing.
I have found a number of solutions to this problem, none of which I am terribly happy with
- Use conditional expressions to either
- Add a test to the assembly – too messy
- change the scope of a variable so it can be tested – I should not be testing members directly
- Link an internal class into the testing assembly, I haven’t yet tried this – Is this a good habit to get into.
Writing tests before I write code gives me a much better view of the problem because I must think of it “externally” before I bother with it’s implementation, I am forced to think up a test that boils the whole problem down to an assertion, when that assertion stops being false, my job is done.