单元测试最佳实践? / 测试时 VBNET 2.0 的 C# InternalsVisibleTo() 属性?
我正在 VBNET 2.0 中构建一个 Active Directory 包装器(不能使用以后的 .NET),其中包含以下内容:
- IUtilisateur
- IGroupe
- 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:
- IUtilisateur
- IGroupe
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,InternalsVisibleTo 属性在 vb.net 中可用并且适用于 Friend 类型。
Yes, the InternalsVisibleTo attribute is available in vb.net and works on Friend types.