How to test internal class library?

别等时光非礼了梦想. 提交于 2019-11-27 18:56:09

In .NET can use the InternalsVisibleToAttribute in your class library to make your internal types visible to your unit test project.

That way you can keep your class internal and still use it from other assemblies that you give access.

You use it like this:

[assembly:InternalsVisibleTo("NameOfYourUnitTestProject")]

For the latest csproj 2017 formated projects, if your project does not have the AssemblyInfo.cs file, you can add the following:

  <ItemGroup>
    <AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
      <_Parameter1>$(MSBuildProjectName).Tests</_Parameter1>
    </AssemblyAttribute>
  </ItemGroup>

You also can use other variables to relplace MSBuildProjectName such as AssemblyName or use the unittest project name directly.

You can check the ProjectName.AssemblyInfo.cs in obj folder (obj\Debug\netstandard2.0) has been updated by adding InternalsVisibleTo.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!