Unit test a class declared friend (internal)

僤鯓⒐⒋嵵緔 提交于 2019-12-08 17:40:07

问题


In some of my test helper code, I have a IDbSet(Of T) implementation called FakeDbSet(Of T) that mocks a lot of EF behavior without an actual database. I have the class declared Friend because I want to force all code to interact with it like an IDbSet(Of T). Internally (ie within my Testing assembly), I access a lot of the public members of the class which I want to be able to test.

The problem is, I keep my unit tests in a separate assembly Test.Unit which means they cannot access my internal implementation! I know I can get around this with reflection, but that gets cumbersome and messy very quickly. Is there any better way to set up my project to avoid this problem?


回答1:


Add this to your AssemblyInfo.cs file:

[assembly:InternalsVisibleTo("YourOtherAssembly")]

This will make your internal assemblies visible to YourOtherAssembly. However, you should really consider if you need to have it internal. When writing tests try focusing on your public parts.

Read more here: http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute.aspx



来源:https://stackoverflow.com/questions/13601557/unit-test-a-class-declared-friend-internal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!