I have a separate class library that I’m using for all of my unit testing (using nunit). In the test project, I have a helper class that creates test objects for me. When setting up these objects, I need to set some values that I have marked as internal. The reason I do this is because these objects are actually part of a work flow so I would only otherwise be able to achieve these property states by actually going through the workflow which kind of kills the idea of unit testing.
For instance, the method I’m testing first. It checks to see if a certain approval was made and if it does, it moves it to the next step in the chain (or rather, it checks to see if the next approval was received, and so on down the chain). If it doesn’t not, it reassigns it to the person from whom the approval is needed.
So what I do is create a test ticket that is in a certain stage of this process. For instance, the first test runs the method where the first approver has not approved, and checks to ensure that it gets reassigned to that person. The next test will run it where the approver HAS approved and ensures that it moves on down the tree.
The problem that I obvious face is that the test classes are in a different namespace, so I can’t hit the internal properties. I’m not sure if there’s a work around for this or if maybe I’m missing some other fundamental that is backing me into this corner (design smell?). I realize that you generally only want to write tests for the public api, but part of my tests require that I do some setting of private fields.
Any suggestions?
You could make use of the InternalsVisibleTo attribute to specify that your UnitTest assembly can access internal properties of that class.
This tightly couples your test and ‘real’ assembly but this should get you working.