views:

294

answers:

1

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?

+4  A: 

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

Joel Coehoorn
It works in .NET 2.0? What assembly should I then import to make use of it?
Will Marcouiller
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
And is this a good practice for testing, or is there any other better solution?
Will Marcouiller
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
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
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
@Joel Coehoorn: Thanks for this information! =)
Will Marcouiller
@btlog: Thanks! =)
Will Marcouiller