当 InternalsVisibleToAttribute 不是一个选项时,如何使用反射对程序集中的内部(VB 中的 Friend)类进行单元测试?
我有一个包含两个项目的解决方案:
Company.Project.vbproj
Company.Project.Tests.vbproj
在 Company.Project.vbproj
程序集中,我有一个类 FriendClass.vb
,其范围是 Friend (C# 中的内部) )
。
现在我希望在 Company.Project.Tests.vbproj
程序集中测试这个 FriendClass.vb
。我知道 InternalsVisibleToAttribute
,但这不是 Visual Basic .NET 2.0 中的一个选项,因为它仅适用于 .NET 2.0 中的 C# (请参阅此处)。
我想在我的测试程序集中使用这个内部 FriendClass 为自己创建一个代理类,以便我可以实例化它并相应地进行测试。
有任何想法或已知的做法吗?
提前致谢! =)
I have a solution with two projects within:
Company.Project.vbproj
Company.Project.Tests.vbproj
Within the Company.Project.vbproj
assembly, I have a class FriendClass.vb
which scope is Friend (internal in C#)
.
Now I wish to test this FriendClass.vb
from within the Company.Project.Tests.vbproj
assembly. I know about the InternalsVisibleToAttribute
, but that is not an option in Visual Basic .NET 2.0, as it is only available with C#, in .NET 2.0 (see here).
I would like to create myself a proxy class using this internal FriendClass from within my testing assembly, so that I could instantiate it and do the testings accordingly.
Any idea or known practices to do so?
Thanks in advance! =)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现的唯一解决方法是 .NET Framework 1.1 中使用的方法。
由于
InternalsVisibleToAttribute
在 .NET 2.0 Visual Basic 中不可用,我发现的唯一解决方法是将我的测试包含在与我的库本身相同的项目中。此外,还需要完成一些进一步的工作。#if CONFIG = "Tests" then ... #end if
;例如,如果我有以下 Friend 类:
那么,如果您想在 .NET 2.0 Visual Basic 中测试该类,则需要在
MyFactory
类所在的同一项目中创建一个测试类坐着。此类应如下所示:由于您有一个编译器指令告诉编译器仅在选择“测试”配置时才编译并包含此类,因此您不会在“调试”或“发布”模式下获得此类。这个类甚至不会成为库的一部分,这样不会不必要地污染您的库,并且这允许您无论如何测试您的 Friend 类。
这是我发现在 Visual Basic .NET 2.0 中解决此问题的最明智的方法。
The only workaround that I have found is one used back in .NET Framework 1.1.
As the
InternalsVisibleToAttribute
is not useable in .NET 2.0 Visual Basic, the only workaround that I have found is to include my tests within the same project as my library itself. Besides, some further work needs to be accomplished.#if CONFIG = "Tests" then ... #end if
;For example, if I have the following Friend class:
Then, if you want to test this class in .NET 2.0 Visual Basic, you will need to create a test class within the same project where the
MyFactory
class sits. This class should look like so:Since you have a compiler directive telling the compiler to compile and to include this class only when the "Tests" CONFIG is selected, you won't get this class on "Debug" or on "Release" mode. This class won't even be part of the library, this for not polluting your library unnecessarily, and this allows you to test your Friend class anyway.
That is the smartest way I have found to work around this issue in Visual Basic .NET 2.0.