c# - VS2017 .NET Standard 库中的单元测试内部方法

标签 c# unit-testing visual-studio-2017 .net-standard

我目前正在通过创建 .NET Standard 1.6 库来使用最新的 Visual Studio 2017 Release Candidate。我正在使用 xUnit 对我的代码进行单元测试,想知道您是否仍然可以在 VS2017 中测试内部方法。

我记得你可以在 VS2015 的 AssemblyInfo.cs 类中添加一行,使指定的项目能够看到内部方法:

[assembly:InternalsVisibleTo("MyTests")]

由于 VS2017 .NET Standard 项目中没有 AssemblyInfo.cs 类,我想知道您是否仍然可以对内部方法进行单元测试?

最佳答案

根据 .NET docs for the InternalsVisibleToAttribute :

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 文件中,它应该可以正常工作:

// some .cs file included in your project
using System.Runtime.CompilerServices;
[assembly:InternalsVisibleTo("MyTests")]

关于c# - VS2017 .NET Standard 库中的单元测试内部方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42235401/

相关文章:

c# - 大型数组中的内存泄漏 - 子类化 IList 会修复它吗?

c# - C# 中的 SATO USB 打印

java - 如何为不在设备或模拟器上的 Android 运行单元测试?

java - 我应该在这里测试什么?

msbuild - 自 Visual Studio 2017 版本 15.8 以来不断构建失败(Roslyn/CodeAnalysis)。有任何想法吗?

c# - 对 .net 框架解决方案中的子项目使用较新的目标框架 .net 标准

c# - 将对数据库所做的更改存储在 Entity Framework 中

java - PowerMockito : doThrow unfinished stubbing

asp.net-core - 在发布时替换 asp.net core 中的 web.config vs 2017

c# - foreach 循环为 mvc View 中的所有帖子显示相同的图像