Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I would like to write a class library which creates for me a complex object but should only be exposed as little as possible. I want it to be included into other projects and there I only have one call to this library which e.g. returns me an object of a internally created class. I don't 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 sub classes to create the requested instance and sets its properties accordingly. Therefore I want to assure somehow by tests that the whole creation process worked fine. But since I also don't want to expose very little internal properties of the result object too I cannot test by only using this public access method since I don't have any properties to assert on.

I know that you should not test for internal mechanics and it is typically bad design and I also was reading through this article, but isn't there maybe any way to create a library plus unit test project and maybe afterwards limit the access to this class? with a wrapper or something?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
127 views
Welcome To Ask or Share your Answers For Others

1 Answer

In .NET, you can use the InternalsVisibleToAttribute in your class library to make your internal types visible to your unit test project.

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

You use it like this:

[assembly:InternalsVisibleTo("NameOfYourUnitTestProject")]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...