当 InternalsVisibleToAttribute 不是一个选项时,如何使用反射对程序集中的内部(VB 中的 Friend)类进行单元测试?

发布于 2024-09-16 13:22:53 字数 846 浏览 1 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌若浮生 2024-09-23 13:22:53

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

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

  1. 为自己创建一个名为“测试”的新编译配置(您可以在其中选择“发布”/“调试”);
  2. 在项目中创建一个名为“Tests”的新文件夹;
  3. 添加一个新类,用于测试您的 Friend(C# 内部)成员;
  4. 此类中的第一行代码应为:#if CONFIG = "Tests" then ... #end if;
  5. 将您的代码放置在该编译器 IF 指令之间。

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

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 类所在的同一项目中创建一个测试类坐着。此类应如下所示:

#If CONFIG = "Tests" Then

    Imports NUnit.Framework

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

#End If

由于您有一个编译器指令告诉编译器仅在选择“测试”配置时才编译并包含此类,因此您不会在“调试”或“发布”模式下获得此类。这个类甚至不会成为库的一部分,这样不会不必要地污染您的库,并且这允许您无论如何测试您的 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.

  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.

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文