[英]C# unit test code questions continue
more questions after questions in here: C# unit test code questions 在这里,问题后面还有更多问题: C#单元测试代码问题
I found the VS unit test testframe treat private
and protected
method in the same way but deferent from public
method. 我发现VS单元测试测试框架以相同的方式对待private
方法和protected
方法,但不同于public
方法。
The following is the generated code for a private
method: 以下是为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.");
}
questions: 问题:
[DeploymentItem("SystemSoftware.exe")]
is for private
and protected
methods, why needs it and what is it for? [DeploymentItem("SystemSoftware.exe")]
用于private
和protected
方法,为什么需要它,它的用途是什么?
In my original class/file, if I point to the original method and try to " Find All References
". 在我的原始类/文件中,如果我指向原始方法并尝试“ Find All References
”。 The reference in the unit test class/file will not show up for private
and protected
methods but it will show up for all public
methods. 单元测试类/文件中的引用不会针对private
和protected
方法显示,但会针对所有public
方法显示。 Why is that? 这是为什么? Is it right? 这样对吗?
[DeploymentItem("SystemSoftware.exe")] is for private and protected methods, why needs it and what is it for? [DeploymentItem(“ SystemSoftware.exe”)]用于私有和受保护的方法,为什么需要它,它的用途是什么?
You can't access private, protected or internal members from a unit test which is in a different assembly and doesn't inherit from the class you're trying to test (nor would it be possible if your "unit" to be tested is more than a single class). 您不能从单元测试中访问私有成员,受保护成员或内部成员,单元测试位于不同的程序集中,并且不能从您要测试的类继承(如果要测试的“单元”也不能多于一个班级)。 To have access to private, protected or internal members, the MSTest framework will generate an accessor assembly that gives you proxies to access these otherwise hidden members. 若要访问私有成员,受保护成员或内部成员,MSTest框架将生成一个访问器程序集,该程序集使您可以代理访问这些原本隐藏的成员。
The DeploymentItemAttribute signals the test runner which artifacts (and dependencies such as accessor assemblies or test data files) need to be deployed so the code can be properly execute. DeploymentItemAttribute向测试运行程序发出信号,指示需要部署哪些工件(以及相关性,例如访问器程序集或测试数据文件),以便可以正确执行代码。 In essence it implicitly tells the MSTest framework to generate and deploy an accessor assembly in this case. 本质上,在这种情况下,它隐式告诉MSTest框架生成和部署访问器程序集。
In my original class/file, if I point to the original method and try to "Find All References". 在我的原始类/文件中,如果我指向原始方法并尝试“查找所有引用”。 The reference in the unit test class/file will not show up for private and protected methods but it will show up for all public methods. 单元测试类/文件中的引用不会针对私有和受保护的方法显示,但会针对所有公共方法显示。 Why is that? 这是为什么? Is it right? 这样对吗?
See above, you're not directly accessing them but use a proxy to do so. 参见上文,您不是直接访问它们,而是使用代理来访问它们。 This proxy uses runtime reflection to bind your call, so this can't be traced inside Visual Studio. 该代理使用运行时反射来绑定您的调用,因此无法在Visual Studio中进行跟踪。
[DeploymentItem("SystemSoftware.exe")] is for private and protected methods, why needs it and what is it for? [DeploymentItem(“ SystemSoftware.exe”)]用于私有和受保护的方法,为什么需要它,它的用途是什么?
It defines (file) resources that test needs (you can apply to the test class or individual methods). 它定义(文件)测试需求的资源(您可以应用于测试类或单个方法)。 Since test methods must be public I cannot see why you would apply this to a private
or protected
method. 由于测试方法必须是公开的,所以我看不到您为何将其应用于private
或protected
方法。
The attribute is documented: http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.deploymentitemattribute.aspx 该属性已记录: http : //msdn.microsoft.com/zh-cn/library/microsoft.visualstudio.testtools.unittesting.deploymentitemattribute.aspx
Find All References
won't find those tests because they use an accessor class ( User_Accessor
) instead of the real class ( User
) to access protected
and private
methods. Find All References
将找不到这些测试,因为它们使用访问器类( User_Accessor
)而不是实际类( User
)来访问protected
private
方法。 The accessor class is automatically generated and does some tricks to expose those normally not accessable methods. 访问器类是自动生成的,并且会执行一些技巧来公开那些通常不可访问的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.