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

问题描述

我们将所有单元测试放在了自己的项目中。我们发现,对于单元测试,我们必须公开某些类而不是内部类。无论如何要避免这样做。

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,则程序集属性使您可以创建朋友程序集。这些是特定的强命名程序集,允许它们访问内部类和另一个程序集的成员。

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