gpt4 book ai didi

C#单元测试代码题继续

转载 作者:行者123 更新时间:2023-11-30 21:21:26 25 4
gpt4 key购买 nike

这里有更多问题:C# unit test code questions

我发现 VS 单元测试测试框架以相同的方式对待 privateprotected 方法,但不同于 public 方法。

以下是为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. 在我的原始类/文件中,如果我指向原始方法并尝试“查找所有引用”。单元测试类/文件中的引用不会出现在 privateprotected 方法中,但它会出现在所有 public 方法中。这是为什么?对吗?

最佳答案

[DeploymentItem("SystemSoftware.exe")] is for private and protected methods, why needs it and what is it for?

您不能从单元测试中访问私有(private)成员、 protected 成员或内部成员,该单元测试位于不同的程序集中并且不继承您要测试的类(如果您的“单元”是被测试的不止一个类)。要访问私有(private)成员、 protected 成员或内部成员,MSTest 框架将生成一个访问程序集,为您提供访问这些隐藏成员的代理。

DeploymentItemAttribute 向测试运行器发出信号,表明需要部署哪些工件(以及访问器程序集或测试数据文件等依赖项),以便代码可以正确执行。本质上,它隐式地告诉 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?

见上文,您不是直接访问它们,而是使用代理来访问它们。此代理使用运行时反射来绑定(bind)您的调用,因此无法在 Visual Studio 中对其进行跟踪。

关于C#单元测试代码题继续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2714923/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com