4

I'm building an Active Directory wrapper in VBNET 2.0 (can't use later .NET) in which I have the following:

  1. IUtilisateur
  2. IGroupe
  3. IUniteOrganisation

These interfaces are implemented in internal classes (Friend in VBNET), so that I want to implement a façade in order to instiate each of the interfaces with their internal classes. This will allow the architecture a better flexibility, etc.

Now, I want to test these classes (Utilisateur, Groupe, UniteOrganisation) in a different project within the same solution. However, these classes are internal. I would like to be able to instantiate them without going through my façade, but only for these tests, nothing more.

Here's a piece of code to illustrate it:

public static class DirectoryFacade {
    public static IGroupe CreerGroupe() {
        return new Groupe();
    }
}

// Then in code, I would write something alike:

public partial class MainForm : Form {
    public MainForm() {
        IGroupe g = DirectoryFacade.CreerGroupe();
        // Doing stuff with instance here...
    }
}

// My sample interface:

public interface IGroupe {
    string Domaine { get; set; }
    IList<IUtilisateur> Membres { get; }
}

internal class Groupe : IGroupe {
    private IList<IUtilisateur> _membres;

    internal Groupe() {
        _membres = new List<IUtilisateur>();
    }

    public string Domaine { get; set; }
    public IList<IUtilisateur> Membres {
        get {
            return _membres;
        }
    }
}

I heard of InternalsVisibleTo() attribute, recently. I was wondering whether it is available in VBNET 2.0/VS2005 so that I could access the assmebly's internal classes for my tests? Otherwise, how could I achieve this?

EDIT Is this a good testing practice to proceed like I do?

Will Marcouiller
  • 23,773
  • 22
  • 96
  • 162

1 Answers1

5

Yes, the InternalsVisibleTo attribute is available in vb.net and works on Friend types.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • It works in .NET 2.0? What assembly should I then import to make use of it? – Will Marcouiller May 13 '10 at 13:23
  • It's in `System.Runtime.CompilerServices`. More info here: http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute(VS.90).aspx?ppud=4 – Joel Coehoorn May 13 '10 at 13:32
  • And is this a good practice for testing, or is there any other better solution? – Will Marcouiller May 13 '10 at 13:42
  • 1
    I think it's still under debate whether or not this is the best practice, pending other techniques like IoC, but it's certainly a _common_ practice. – Joel Coehoorn May 13 '10 at 14:13
  • 1
    Whether it is good practice is a bit of a philosophical argument. For me a lot of this depends on whether it is an app internal to an organisation, released software or API – btlog May 13 '10 at 14:14
  • I used the InternalVisibleAttribute, but it doesn't seem to work with internal classes. I can't instantiate my class Groupe from my Tests project. =( – Will Marcouiller May 13 '10 at 14:20