I am trying to use the InternalsVisibleTo
assembly attribute to make my internal classes in a .NET class library visible to my unit test project. For some reas
1- Sign the test project: In Visual Studio go to the properties window of the test project and Sign the assembly by checking the checkbox with the same phrase in the Signing tab.
2- Create a PublicKey for the test project: Open Visual Studio Command Prompt (e.g. Developer Command Prompt for VS 2017). Go to the folder where the .dll file of the test project exists. Create a Public Key via sn.exe:
sn -Tp TestProject.dll
Note that the argument is -Tp, but not -tp.
3- Introduce the PublicKey to the project to be tested: Go to the AssemblyInfo.cs file in the project to be tested and add this line with the PublicKey created in the previous step:
[assembly: InternalsVisibleTo("TestProjectAssemblyName, PublicKey=2066212d128683a85f31645c60719617ba512c0bfdba6791612ed56350368f6cc40a17b4942ff16cda9e760684658fa3f357c137a1005b04cb002400000480000094000000060200000024000052534131000400000100010065fe67a14eb30ffcdd99880e9d725f04e5c720dffc561b23e2953c34db8b7c5d4643f476408ad1b1e28d6bde7d64279b0f51bf0e60be2d383a6c497bf27307447506b746bd2075")]
Don't forget to replace the above PublicKey with yours.
4- Make the private method internal: In the project to be tested change the access modifier of the method to internal.
internal static void DoSomething(){...}