问题描述
我是单元测试的新手,我想知道是否应该开始使用更多的 internal
访问修饰符.我知道如果我们使用 internal
并设置程序集变量 InternalsVisibleTo
,我们可以测试我们不想从测试项目中声明为 public 的函数.这让我觉得我应该总是使用 internal
因为至少每个项目(应该?)都有自己的测试项目.你们能告诉我为什么我不应该这样做吗?我什么时候应该使用 private
?
I'm new to unit testing and I'm trying to figure out if I should start using more of internal
access modifier. I know that if we use internal
and set the assembly variable InternalsVisibleTo
, we can test functions that we don't want to declare public from the testing project. This makes me think that I should just always use internal
because at least each project (should?) have its own testing project. Can you guys tell me why I shouldn't do this? When should I use private
?
推荐答案
内部类需要测试,有一个assemby属性:
Internal classes need to be tested and there is an assemby attribute:
using System.Runtime.CompilerServices;
[assembly:InternalsVisibleTo("MyTests")]
将此添加到项目信息文件中,例如PropertiesAssemblyInfo.cs
.
Add this to the project info file, e.g. PropertiesAssemblyInfo.cs
.
这篇关于C#“内部"进行单元测试时的访问修饰符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!