Unit Testing using InternalsVisibleToAttribute requires compiling with /out:filename.ext?

时光怂恿深爱的人放手 提交于 2019-12-10 19:05:22

问题


In my most recent question: Unit Testing Best Practice? / C# InternalsVisibleTo() attribute for VBNET 2.0 while testing?, I was asking about InternalsVisibleToAttribute.

I have read the documentation on how to use it, and everything is fine and understood. However, I can't instantiate my class Groupe from my Testing project.

I want to be able to instantiate my internal class in my wrapper assembly, from my testing assembly.

Any help is appreciated!

EDIT #1

Here's the compile-time error I get when I do try to instantiate my type:

Erreur 2 'Carra.Exemples.Blocs.ActiveDirectory.Groupe' n'est pas accessible dans ce contexte, car il est 'Private'. C:\Open\Projects\Exemples\Src\Carra.Exemples.Blocs.ActiveDirectory\Carra.Exemples.Blocs.ActiveDirectory.Tests\GroupeTests.vb 9 18 Carra.Exemples.Blocs.ActiveDirectory.Tests

(This says that my type is not accessible in this context, because it is private.) But it's Friend (internal)!

EDIT #2

Here's a piece of code as suggested for the Groupe class implementing the Public interface IGroupe:

#Region "Importations"
Imports System.DirectoryServices
Imports System.Runtime.CompilerServices
#End Region

<Assembly: InternalsVisibleTo("Carra.Exemples.Blocs.ActiveDirectory.Tests")> 

Friend Class Groupe
    Implements IGroupe

#Region "Membres privés"
    Private _classe As String = "group"
    Private _domaine As String
    Private _membres As CustomSet(Of IUtilisateur)
    Private _groupeNatif As DirectoryEntry
#End Region

#Region "Constructeurs"
    Friend Sub New()
        _membres = New CustomSet(Of IUtilisateur)()
        _groupeNatif = New DirectoryEntry()
    End Sub

    Friend Sub New(ByVal domaine As String)
        If (String.IsNullOrEmpty(domaine)) Then Throw New ArgumentNullException()
        _domaine = domaine
        _membres = New CustomSet(Of IUtilisateur)()
        _groupeNatif = New DirectoryEntry(domaine)
    End Sub

    Friend Sub New(ByVal groupeNatif As DirectoryEntry)
        _groupeNatif = groupeNatif
        _domaine = _groupeNatif.Path
        _membres = New CustomSet(Of IUtilisateur)()
    End Sub
#End Region

And the code trying to use it:

#Region "Importations"
Imports NUnit.Framework

Imports Carra.Exemples.Blocs.ActiveDirectory
#End Region

<TestFixture()> _
Public Class GroupeTests
    <Test()> _
    Public Sub CreerDefaut()
        Dim g As Groupe = New Groupe()
        Assert.IsNotNull(g)
        Assert.IsInstanceOf(Groupe, g)
    End Sub
End Class

EDIT #3

Damn! I have just noticed that I wasn't importing the assembly in my importation region.

Nope, didn't solve anything =(

Thanks!


回答1:


It should work already. InternalsVisibleTo makes all internal members of an assembly visible - including internal types.

What happens when you do try to instantiate your class from your test project?

EDIT: Is the constructor itself Friend/internal? You don't just need access to the type - you need access to the constructor as well. Admittedly it doesn't look like that's the problem from the compiler error, but I'm not sure.

Other things to check:

  • Is InternalsVisibleTo working for you for other internal members in this pair of assemblies? Could it just be that you haven't applied InternalsVisibleTo properly?
  • Is Groupe a nested type inside a private type? That would explain the problem.

If these don't help, it would be good if you could post a very short but complete example which fails to work - just a Friend type in one assembly (along with the InternalsVisibleTo attribute) and another piece of code which tries to use it.




回答2:


After researching and researching, still researching and guess what? Researchain again, I have found a link where it is said that 'InternalsVisibleTo' was not available for VB.NET, thought the attribute was available in .NET 2.0. Here's the link in question:

InternalsVisibleTo: Testing internal methods in .Net 2.0

The Remark states:

In the .NET Framework version 2.0, Visual Basic does not support the use of this attribute.

So it is not available, that's all! =)




回答3:


is your test assembly called Carra.Exemples.Blocs.ActiveDirectory.Tests?

you have imported Carra.Exemples.Blocs.ActiveDirectory.Tests, but surely you should just be importing Carra.Exemples.Blocs.ActiveDirectory? What is the assembly that your class is in called? Seems like something fishy going on.



来源:https://stackoverflow.com/questions/2827551/unit-testing-using-internalsvisibletoattribute-requires-compiling-with-outfile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!