本文介绍了使代码在内部但可用于其他项目的单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们将所有的单元测试放在他们自己的项目中.我们发现我们必须为单元测试公开某些类而不是内部类.有没有办法避免这样做.将类设为公开而不是密封会带来哪些内存影响?

We put all of our unit tests in their own projects. We find that we have to make certain classes public instead of internal just for the unit tests. Is there anyway to avoid having to do this. What are the memory implication by making classes public instead of sealed?

推荐答案

如果您使用 .NET,InternalsVisibleTo 程序集属性允许您创建朋友"程序集.这些是特定的强命名程序集,允许访问其他程序集的内部类和成员.

If you're using .NET, the InternalsVisibleTo assembly attribute allows you to create "friend" assemblies. These are specific strongly named assemblies that are allowed to access internal classes and members of the other assembly.

注意,这应该谨慎使用,因为它紧密耦合了所涉及的程序集.InternalsVisibleTo 的一个常见用途是用于单元测试项目.由于上述原因,它可能不是在您的实际应用程序程序集中使用的好选择.

Note, this should be used with discretion as it tightly couples the involved assemblies. A common use for InternalsVisibleTo is for unit testing projects. It's probably not a good choice for use in your actual application assemblies, for the reason stated above.

示例:

[assembly: InternalsVisibleTo("NameAssemblyYouWantToPermitAccess")]
namespace NameOfYourNameSpace
{

这篇关于使代码在内部但可用于其他项目的单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 23:36