How to test the internal class library?

I would like to write a class library that creates a complex object for me, but should only be shown as little as possible. I want it to be included in other projects, and there is only one call to this library, for example, it returns an object of the inner class to me. I do not want to allow others to create these objects explicitly, but still I want to create a test project for this class library.

For example:

var result = Manager.Instance.Create(definition) 

This should be the only access to the class library.

Based on the definition parameter, it uses different subclasses to create the requested instance and sets its properties accordingly. Therefore, I want to somehow assure in the tests that the entire creation process worked fine. But since I also do not want to expose very small internal properties of the result object, I cannot test using this open access method, since I do not have any properties for approval.

I know that you should not test internal mechanics, and this is usually a bad design, and I also read this article , but maybe there is no library to create plus a unit test project and, possibly, subsequently restrict access to this class? with a wrapper or something?

+48
c # unit-testing wrapper internal
Mar 15 '13 at 19:47
source share
1 answer

In .NET, you can use InternalsVisibleToAttribute in your class library so that your internal types are visible in your unit test project.

That way you can keep your class internal and still use it from other assemblies that you provide.

You use it as follows:

 [assembly:InternalsVisibleTo("NameOfYourUnitTestProject")] 
+121
Mar 15 '13 at 19:50
source share



All Articles