How should internal classes and methods be tested? [duplicate]
Possible Duplicate:
testing an internal class
I like to have my test classes in a separate assembly, and that is causing some problem with testing internal classes. Si开发者_Go百科nce internal classes and methods are only visible inside of the assembly my test assembly can't see those classes and methods. What is the most effective way to test those classes? Do I need to use reflection to access the methods I wan't to test?
You can use the InternalsVisbleTo attribute.
See this question: How do I allow assembly (unit testing one) to access internal properties of another assembly?
My preferred option is to just test them indirectly via the public classes and methods that use them, just like with private classes and methods.
Depends though, I tend to do TDD, and end up hardly ever using internal. I don't really like the idea of putting an attribute in production code just to make it testable.
You could refactor the functionality into new class with a public method and then test that method.
I think you may question where is your internal class is used and for what purpose.
If it is there it must have its own mission. And that mission might be effecting something; for instance another object's state.
So by testing that 'another' object you might have a chance to test your internal class indirectly.
But the good start can be questioning if it is really necessary to have that internal class. If your intention is to write a TTDD (Testable Test Driven Development) your design might require another architecture.
精彩评论