Language: EN

como-hacer-test-unitarios-de-metodos-internal-en-net

How to perform unit tests on Internal methods in .NET

One of the challenges when performing unit tests in .NET is that the methods we want to test need to be public.

However, in many cases, we will want to apply unit tests to methods defined as Internal. In this case, we will not be able to perform unit tests.

Fortunately, we can make Internal methods Public for certain assemblies, which we will call “Friend Assemblies”.

To do this, we must edit the project file that we want to test. Inside it, we will add the following line, replacing ‘MyAssemblyTests’ with the name of the Test project’s assembly.

  <ItemGroup>
    <AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
      <_Parameter1>MyAssemblyTests</_Parameter1>
    </AssemblyAttribute>
  </ItemGroup>

So, for example, the project file would look like this.

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
      <_Parameter1>MyAssemblyTests</_Parameter1>
    </AssemblyAttribute>
  </ItemGroup>
</Project>