VS2017 .NET Standard 库中的单元测试内部方法 [英] 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 test internal methods in VS2017.
我记得你可以在 VS2015 的 AssemblyInfo.cs 类中添加一行,使指定的项目能够看到内部方法:
I remember that you could add a line in AssemblyInfo.cs class in VS2015 that would enable specified projects to see internal methods:
[assembly:InternalsVisibleTo("MyTests")]
由于 VS2017 .NET Standard 项目中没有 AssemblyInfo.cs 类,我想知道您是否仍然可以对内部方法进行单元测试?
As there is no AssemblyInfo.cs class in VS2017 .NET Standard projects, I was wondering if you can still unit test internal methods?
推荐答案
根据 InternalsVisibleToAttribute 的 rel="noreferrer">.NET 文档:
According to .NET docs for the 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 Standard 库中的单元测试内部方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!