Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit test a class declared friend (internal)

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?

like image 952
just.another.programmer Avatar asked Nov 28 '12 09:11

just.another.programmer


1 Answers

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

like image 175
Tomas Jansson Avatar answered Nov 03 '22 11:11

Tomas Jansson