2010-04-26 54 views
2

更多的问题:C# unit test code questionsC#单元测试代码问题继续在这里提问后

我发现VS单元测试testframe从public方法治疗以同样的方式privateprotected方法,但输精管。

下面是一个private方法生成的代码:

 /// <summary> 
     ///A test for recordLogin 
     ///</summary> 
     [TestMethod()] 
     [DeploymentItem("SystemSoftware.exe")] 
     public void recordLoginTest() 
     { 
      User_Accessor target = new User_Accessor(); // TODO: Initialize to an appropriate value 
      Guid userId = new Guid(); // TODO: Initialize to an appropriate value 
      string action = string.Empty; // TODO: Initialize to an appropriate value 
      Users user = null; // TODO: Initialize to an appropriate value 
      AndeDBEntities db = null; // TODO: Initialize to an appropriate value 
      bool expected = false; // TODO: Initialize to an appropriate value 
      bool actual; 
      actual = target.recordLogin(userId, action, user, db); 
      Assert.AreEqual(expected, actual); 
      Assert.Inconclusive("Verify the correctness of this test method."); 
     } 

问题:

  1. [DeploymentItem("SystemSoftware.exe")]privateprotected方法,为什么需要它,它是什么呢?

  2. 在我的原始类/文件中,如果我指向原始方法并尝试“Find All References”。单元测试类/文件中的引用不会显示为privateprotected方法,但会显示所有方法的public方法。这是为什么?这样对吗?

回答

1

[DeploymentItem( “SystemSoftware.exe”)是private和protected方法,为什么需要它,它是什么呢?

它定义(文件)测试需求的资源(可以应用到测试类或单个方法)。由于测试方法必须公开,所以我不明白你为什么会将此应用于privateprotected方法。

属性被记载:因为他们使用的访问类(User_Accessor),而不是真正的类(User)来访问protectedprivate方法http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.deploymentitemattribute.aspx

1

Find All References不会发现这些测试。访问器类是自动生成的,并执行一些技巧来公开那些通常不可访问的方法。

2

[DeploymentItem(“SystemSoftware.exe”) 是私有和保护方法, 为什么需要它,它是什么呢?

您不能访问私有,保护或从单元测试是在一个不同的组件,并且不从你试图测试(也如果有可能的类继承内部成员的“单元“进行测试不止是一个班级)。要访问私有,受保护或内部成员,MSTest框架将生成访问程序集,该访问程序集允许代理访问这些隐藏的成员。

DeploymentItemAttribute指示测试运行器需要部署哪些工件(以及依赖项(如访问器程序集或测试数据文件)),以便代码可以正确执行。实质上,它隐含地告诉MSTest框架在这种情况下生成和部署访问器程序集。

我在原来的类/文件,如果我点 原来的方法,并尝试 “查找所有引用”。参考 在单元测试类/文件将不会 显示为私人和受保护的 方法,但它会显示所有 公共方法。这是为什么?它是 对不对?

请参阅上文,您不是直接访问它们,而是使用代理来执行此操作。此代理使用运行时反射来绑定您的调用,因此无法在Visual Studio中进行跟踪。