I have a C# project and a test project with unit tests for the main project. I want to have testable internal
methods and I want to test them without a magical Accessor object that you can have with Visual Studio test projects. I want to use InternalsVisibleToAttribute
but every time I've done it I've had to go back and look up how to do it, which I remember involves creating key files for signing the assemblies and then using sn.exe
to get the public key and so on.
Is there a utility that automates the process of creating a SNK file, setting projects to sign the assemblies, extracting the public key, and applying the InternalsVisibleTo attribute?
Is there a way to use the attribute without signed assemblies?
The assembly attribute InternalsVisibleTo can be utilized in this scenario to unit test individual methods from an external project without exposing said methods on the application's public surface.
Assembly-level attributes can appear in any code file; however, by convention they are placed in the AssemblyInfo file. This aids discoverability, as this is where people (or tools) will commonly look when investigating issues. I have been using InternalsVisibleTo for many years and placing it in the AssemblyInfo file.
You don't have to use signed assemblies to use InternalsVisibleTo
. If you don't use signed assemblies, you can just enter the full name of the assembly.
So if you want to have access to MyAssembly
in you test assembly (MyAssembly.Test
) all you need in AssemblyInfo.cs
for MyAssembly
is the name of the test assembly like this:
[assembly: InternalsVisibleTo("CompanyName.Project.MyAssembly.Test")]
Brian is right, but, offcourse in some cases you do have a signed assembly, and you do want to get the public key of that assembly.
Getting this key is indeed quite a hassle.
Therefore, I've done this:
I've customized my external tools in VS.NET, so that I can get the public key of the assembly of the current project with just one click of the mouse.
This is how it's done:
C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\sn.exe
-Tp $(TargetPath)
Then, you build the project, and when you click on 'Get public key', you should see the public key for this project in the output window.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With