本文介绍了使用“朋友"声明进行单元测试.馊主意?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[当然,问题不限于特定的朋友"实现,请随时指出相关的实现细节]

通读未解决的问题,我偶然发现了 InternalsVisibleTo 属性:

C#编程指南: //msdn.microsoft.com/en-us/library/"rel =" noreferrer> MSDN 的朋友程序集描述如何使用该属性来允许将internal方法和类型用于另一个程序集.

我想知道使用它创建一个隐藏的"接口来检测用于单元测试程序集的库是否是一个好主意.它似乎在两个方向上都大大增加了耦合(生产程序集中的测试代码,测试代码中对生产程序集的深入内部知识),但是另一方面,它可能有助于创建细粒度的测试而不会弄乱公共接口. /p>

您在测试时使用朋友声明的经历是什么?是您的银弹,还是开始了死亡游行?

解决方案

我已经广泛使用了这种技术-这意味着我的单元测试可以测试普通用户看不见的代码库方面. /p>

虽然使用[InternalsVisibleTo]确实会增加耦合,但我相信(较小的)增加是值得的.

我的单元测试已经与要测试的代码紧密结合-尽管我尝试编写测试来确保特定的结果(而不是特定的实现),方法是访问常规使用者不可见的内容,但是我确实在一定程度上限制了实现.

相反,耦合是最小的-在代码汇编中具有[InternalsVisibleTo]属性,并将某些内容标记为内部而不是私有(或受保护的内部,而不是受保护的).

(请注意,我在这里忽略了由于使用单元测试而引起的任何设计更改,这是另外一个完整的讨论.)

[InternalsVisibleTo]属性要求对程序集进行强命名.如果您还没有这样做,您可能会发现这有点麻烦,因为强命名程序集可能仅依赖于其他强命名程序集,最终可能需要更改多个程序集.

正确设置属性可能有点麻烦,因为它需要包含测试程序集的公钥. IDesign具有一个有用的朋友装配工具,它可以创建剪贴板上的属性,准备粘贴.推荐.

[Of course, the question is not restricted to a specific "friend" implementation, feel free though to point out implementation specifics if relevant]

Reading through the unanswered questions, I stumbled upon the InternalsVisibleTo attribute:

The C# Programming Guide on MSDN has a section Friend Assemblies describing how to use the attribute to allow the use of internal methods and types to another assembly.

I'm wondering whether it would be a Good Idea to use this to create a "hidden" interface for instrumenting a library for use by the unit testing assembly. It seems to increase coupling massively in both directions (testing code in the production assembly, intimate internal knowledge about the production assembly in testing code), but on the other hand it might help in creating fine-grained tests without cluttering the public interface.

What is your experience with using friend declarations when testing? Was it your Silver Bullet, or did it start the Death March?

解决方案

I've made extensive use of this technique - it means that my unit tests can test aspects of the code library that aren't visible to normal consumers.

While using [InternalsVisibleTo] does increase coupling, I believe the (minor) increase is well worth the gains.

My unit tests are already tightly coupled to the code under test - though I try to write tests that assure specific outcomes, not specific implementations, by accessing things not visible to regular consumers, I do somewhat constrain the implementation.

Going the other way, the coupling is minimal - in having the [InternalsVisibleTo] attribute on the code assembly, and in marking some things as internal instead of private (or protected internal instead of protected).

(Note that I'm ignoring here any design changes that are provoked by the use of Unit Testing, which is a whole other discussion.)

The [InternalsVisibleTo] attribute requires strong naming your assemblies. If you're not doing this already, you may find this a bit burdensome as a strongly named assembly may only depend on other strongly named assemblies, which may end up with you needing to alter several assemblies.

Getting the attribute right can be a bit fiddly, as it needs to include the public key of your test assembly. IDesign have a useful Friend Assembly tool that creates the attribute on your clipboard, ready for pasting. Recommended.

这篇关于使用“朋友"声明进行单元测试.馊主意?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 18:23