posts - 11 , comments - 4 , trackbacks - 0

Exposing the Internals of an Assembly to Another Unit Testing Assembly

     I have two projects one is production code (called Main), the other is a unit test project to test the real production code (called Main.Test).  Both Main and Main.Test are signed with a strong name key.  To expose the internal stuff to the Main.Test project we have to add an item at the bottom of the AssemblyInfo.cs class (in Main, not Main.Test) which looks similar to this:

(Note:  Names and Public Keys have been changed to protect the innocent, and semi-innocent)

[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Main.Test, PublicKey=1028000004800030940000100602000000240000525341310004
0000010001001312297cb7c20cfb40f4efef6e5e96dcf84fa46db61b43b2bf65cf
49d547d6a5f14ef07f54d6313b39b10ae631e158748b39e2ab4c816b0753f2a
432160dddf06a1a7170d7ab6e4df081784da7a08b37a3a8a47b722ed6cf6c1c
f6896bc709f805a8087a758156171b7deb74a2ded23ce55688f9b2a67bfeedf1
081496da76f4")]

Now you may be thinking to yourself:  "That's great bob, but how in the hell did you get this value for the public key in the first place?"

Good question!  Here is the answer:

1.  Open a visual studio command prompt and enter this command:

(This is using Visual Studio 2008)
C:\Program Files\Microsoft Visual Studio 9.0\VC>sn -p "[RestOfPath]\Main.Test\MainProjectStrongNameKey.snk" "[RestOfPath]\Main.Test\MainProjectStrongNameKey.publickey"

2.  You should then get a message that says "Public key written blah blah blah".

3.  Now in the same command window, enter this command:

C:\Program Files\Microsoft Visual Studio 9.0\VC>sn -tp "[RestOfPath]\Main.Test\MainProjectStrongNameKey.publickey"

Now the command line should spit out the long public key, which you can then use like I did above.  Now you can test your internal members in the Main project from the Main.Test project

Happy testing!

Print | posted on Monday, November 9, 2009 5:39 PM | Filed Under [ Unit Testing ]

Feedback

Comments are closed.
Comments have been closed on this topic.

Powered by: