Tuesday, March 6, 2007

InternalsVisibleTo

At the chat on mocks, I mentioned you can test "internal" methods in your test fixture even if it isn't in the same assembly as the class under test (CUT). I used the wrong terminology (of course) and called it making the assembly friends, so if you spent the last few days googling that, my appologies :P

Testing internal methods comes in handy quite a bit:
1. Testing things that you don't want other assemblies accessing
2. Testing state machines or some other context of a class that you wouldn't otherwise expose
3. Using partial mocks

So, how do you let your test assembly access you internal methods?

Its quite easy, first you'll need your test assembly to have a strong name. Once thats done, you need to add one line to your CUT's AssemblyInfo.cs file.

[assembly: InternalsVisibleTo("TestAssembly, PublicKey=yourpublickey")]

For those of you used to 2003, you give your assemblies key files in the properties of the project and not in the assembly info :)

2 comments:

hammett said...

"you use to 2003" - You mean "used to 2003"?

wunda said...

hammett -- good catch...