本文介绍了当不能选择InternalsVisibleToAttribute时,如何使用反射对组件中的内部(VB中的Friend)类进行单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两个项目的解决方案:

I have a solution with two projects within:

Company.Project.vbproj
Company.Project.Tests.vbproj

Company.Project.vbproj 程序集,我有一个类 FriendClass.vb ,其范围是 Friend(C#内部)

Within the Company.Project.vbproj assembly, I have a class FriendClass.vb which scope is Friend (internal in C#).

现在我想在 Company.Project.Tests.vbproj FriendClass.vb / code>程序集。我知道,但这不是Visual Basic .NET 2.0中的选项,因为它仅适用于.NET 2.0中的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).

我想创建自己一个在我的测试程序集中使用此内部 FriendClass 的代理类,以便我可以实例化它并相应地进行测试。

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.

这样做有什么想法或已知做法吗?

预先感谢! =)

推荐答案

我发现的唯一解决方法是在.NET Framework 1.1中使用的一种解决方法。

The only workaround that I have found is one used back in .NET Framework 1.1.

由于 InternalsVisibleToAttribute 在.NET 2.0 Visual Basic中不可用,所以我发现的唯一解决方法是将测试包含在同一目录中项目作为我的图书馆本身。此外,还需要完成一些进一步的工作。

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.


  1. 为自己创建一个名为 Tests的新编译CONFIG(您可以在其中选择 Release / Debug);

  2. 在项目中创建一个名为 Tests的新文件夹;

  3. 添加一个新类,将其添加到测试您的Friend(在C#内部)成员;

  4. 此类中的第一行代码应为: #if CONFIG = Tests然后... #end if ;

  5. 将代码放在此编译器IF指令之间。

  1. Create yourself a new compilation CONFIG called "Tests" (that is where you may select "Release"/"Debug");
  2. Create a new folder named "Tests" within your project;
  3. Add a new class, the one to test your Friend (internal in C#) members;
  4. First line of code within this class should be: #if CONFIG = "Tests" then ... #end if;
  5. Place your code between this compiler IF directive.

例如,如果我有以下Friend类:

For example, if I have the following Friend class:

Friend Class MyFactory
    Friend Property Property1 As Object
        Get
            Return _field1
        End Get
        Set (ByVal value As Object)
            _field1 = value
        End Set
    End Property

    Friend Sub SomeSub(ByVal param1 As Object)
        ' Processing here...
    End Sub
End Class

然后,如果要在.NET 2.0 Visual Basic中测试此类,则需要在 MyFactory 类所在的同一项目中创建测试类。此类应如下所示:

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:

#If CONFIG = "Tests" Then

    Imports NUnit.Framework

    <TestFixture()> _
    Public Class MyFactoryTests
        <Test()> _
        Public Sub SettingProperty1Test
            ' Doing test here...
        End Sub
    End Class

#End If

由于您有一个编译器指令,告诉编译器仅在选择了 Tests CONFIG后才进行编译并包含此类,因此您不会不能在调试或发布模式下获得此类。此类甚至都不属于库的一部分,这是因为它不会不必要地污染您的库,并且使您无论如何都可以测试Friend类。

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.

那是最聪明的我发现在Visual Basic .NET 2.0中解决此问题的方法。

That is the smartest way I have found to work around this issue in Visual Basic .NET 2.0.

这篇关于当不能选择InternalsVisibleToAttribute时,如何使用反射对组件中的内部(VB中的Friend)类进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 08:03