VS2017 .Net标准库中的单元测试内部方法 [英] Unit testing internal methods in VS2017 .Net Standard library
问题描述
我目前正在通过创建.Net Standard 1.6库来使用最新的Visual Studio 2017 Release Candidate。我正在使用xUnit来对我的代码进行单元测试,并且想知道是否仍然可以在VS2017中测试内部方法。
I am currently playing around with the latest Visual Studio 2017 Release Candidate by creating a .Net Standard 1.6 library. I am using xUnit to unit test my code and was wondering if you can still testing internal methods in VS2017.
我记得您可以在中全部使用AssemblyInfo.cs类VS2015,它将使指定的项目能够查看内部方法
I remember that you could all a line AssemblyInfo.cs class in VS2015 that would enable specified projects to see internal methods
[assembly:InternalsVisibleTo("MyTests")]
由于VS2017 .Net Standard项目中没有AssemblyInfo.cs类,我想知道您是否仍然可以对内部方法进行单元测试? / p>
As there is no AssemblyInfo.cs class in VS2017 .Net Standard projects I was wondering if you can still unit test internal methods?
推荐答案
根据 .NET文档 InternalsVisibleToAttribute
:
该属性在程序集级别应用。这意味着它可以包含在源代码文件的开头,也可以包含在Visual Studio项目的AssemblyInfo文件中。
The attribute is applied at the assembly level. This means that it can be included at the beginning of a source code file, or it can be included in the AssemblyInfo file in a Visual Studio project.
换句话说,您可以简单地将其放在自己的任意命名的.cs文件中,并且应该可以正常工作:
In other words, you can simply place it in your own arbitrarily named .cs file, and it should work fine:
// some .cs file included in your project
using System.Runtime.CompilerServices;
[assembly:InternalsVisibleTo("MyTests")]
这篇关于VS2017 .Net标准库中的单元测试内部方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!