views:

99

answers:

3

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!

+3  A: 

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.

Jon Skeet
I posted the compilation error I get when I do try to instantiate my type.
Will Marcouiller
Shall I sign the assembly? (Which I wish to avoid...)
Will Marcouiller
@Will: Are *either* of the assemblies signed? If either of them are, they'll both need to be.
Jon Skeet
They're not. =)
Will Marcouiller
Any other ideas? I'm lost, I don't know where to search as it is the first time I'm using the InternalsVisibleToAttribute. Thanks! =)
Will Marcouiller
@Will: As Sam asked, is the name of the assembly definitely right? Note that the assembly name and the namespace can be completely different. What file do you get for your test assembly?
Jon Skeet
Here's the filename: Carra.Exemples.Blocs.ActiveDirectory.Tests.dll
Will Marcouiller
@Jon: Thanks for your help!
Will Marcouiller
Do I need to use the InternalsVisibleToAttribute into a class file that is public in order this to work for other types that are internal?
Will Marcouiller
@Will: InternalsVisibleToAttribute is just applied to the whole assembly - it doesn't matter where you put it. It certainly *looks* like what you've got should work...
Jon Skeet
@Jon: I have read, rereading about this attribute on MSDN, that one shall compile with /out:filename.ext?
Will Marcouiller
@Jon: I have tried to make my class public, letting my constructors internal. The result is that it stopped complaining about my type, but the error I get is that my constructors are not accessible in this context. (Everything is as expected so far, except that it's not working.)
Will Marcouiller
@Jon: It seems that this attribute is not available for VBNET (2.0). Unfortunately, this is with what I'm stuck. Please see my answer for the link (if it interests you, of course).
Will Marcouiller
A: 

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.

Sam Holder
Yes. Good point, as in VBNET, the namespaces are not written in the class file, which I dislike.
Will Marcouiller
Damn! I just noticed that I didn't write the Imports instruction for this...
Will Marcouiller
did that fix your issue?
Sam Holder
I would have thought so, but it doesn't seem to change anything. =(
Will Marcouiller
Will, those imports don't look right. I have updated my answer.
Sam Holder
Thanks for this oversation, Sam! This was a badd copy and paste, as the import is stating the correct assembly in my original code.
Will Marcouiller
I have read about compiling in command line with the /out:filename.ext switch. What do you think of that? IT shouldn't be that complicated, in my opinion. =(
Will Marcouiller
@Sam: It seems the attribute is not available for VBNET (2.0).
Will Marcouiller
+1  A: 

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! =)

Will Marcouiller
+1. phew a lot of effort to discover it can't be used.
Sam Holder