单元测试最佳实践? / 测试时 VBNET 2.0 的 C# InternalsVisibleTo() 属性?

发布于 2024-09-01 13:33:39 字数 1325 浏览 2 评论 0原文

我正在 VBNET 2.0 中构建一个 Active Directory 包装器(不能使用以后的 .NET),其中包含以下内容:

  1. IUtilisateur
  2. IGroupe
  3. IUniteOrganization

这些接口是在内部类中实现的(VBNET 中的朋友),因此我想实现一个外观以便用其内部类实例化每个接口。这将使架构具有更好的灵活性等。

现在,我想在同一解决方案的不同项目中测试这些类(Utilisateur、Groupe、UniteOrganization)。然而,这些类是内部的。我希望能够在不通过我的外观的情况下实例化它们,但仅用于这些测试,仅此而已。

下面是一段代码来说明它:

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;
        }
    }
}

最近我听说了InternalsVisibleTo() 属性。我想知道它是否在 VBNET 2.0/VS2005 中可用,以便我可以访问程序集的内部类进行测试?不然我怎么能达到这个目的呢?

编辑这是像我一样进行的良好测试实践吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无悔心 2024-09-08 13:33:39

是的,InternalsVisibleTo 属性在 vb.net 中可用并且适用于 Friend 类型。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文