问题
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