gpt4 book ai didi

c# - TestCleanup() 实现

转载 作者:太空宇宙 更新时间:2023-11-03 22:07:14 24 4
gpt4 key购买 nike

我将如何为这些 [TestMethod()] 编写 [TestCleanup()] 方法。

我有以下 11 个:

当单独运行时,它们通过,当不同时运行时,第一个通过

[TestMethod()]
public void SplitTdsNameTest_SimpleValidName2()
{
string tdsName = "Mr Daniel George Trunley";
MemberName expected = new MemberName("Mr", "Daniel George", "Trunley");
MemberName actual;
actual = TdsTransformer.SplitTdsName(tdsName);
Assert.AreEqual(expected, actual);
}


[TestMethod()]
public void SplitTdsNameTest_SimpleValidName3()
{
string tdsName = "Daniel George Trunley";
MemberName expected = new MemberName("", "", "Daniel George Trunley");
MemberName actual;
actual = TdsTransformer.SplitTdsName(tdsName);
Assert.AreEqual(expected, actual);
}

成员姓名:

public struct MemberName
{
public string Title;
public string FirstNames;
public string LastNames;

public MemberName(string title, string firstNames, string lastNames)
{
Title = title;
FirstNames = firstNames;
LastNames = lastNames;
}
}

split :

public MemberName SplitTdsName(string tdsName)
{
return NameSplitter.Splitter(tdsName);
}

分离器:

public static MemberName Splitter(string fullName)
{
nameInFull = fullName;
SetAllowedTitles();
SplitNamesAndRemovePeriods();
SetTitles();
MemberName splitName = new MemberName(titles, firstNames, lastNames);
return splitName;
}

最佳答案

根据提供的内容,看起来不需要任何清理。

如果有 TestInitialize 方法,只需撤消那里所做的事情。

如果 TdsTransformer.SplitTdsName 存储了任何内部状态,您也必须清除它。尽管如果它,它可能不应该是静态方法。

根据编辑,这两种方法让我担心:

    SetAllowedTitles();
SetTitles();

它们暗示存在内部状态,在我看来,这对于静态类来说非常糟糕。这可能是您看到的连续测试未通过的问题。

要更正这些使其保持静态,让它们返回值而不是存储在静态成员中:

    var allowedTitles = SetAllowedTitles(fullName);
var names = SplitNamesAndRemovePeriods(fullName); //likely a struct or class with first/last names
var titles = SetTitles(allowedTitles);

关于c# - TestCleanup() 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7982801/

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