Unit Testing using InternalsVisibleToAttribute requires compiling with /out:filename.ext?
- by Will Marcouiller
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.Tests
#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!