gpt4 book ai didi

c# - 使用 Given-When-Then 模式的 switch-case 测试方法

转载 作者:行者123 更新时间:2023-11-28 20:29:14 25 4
gpt4 key购买 nike

我有遗留代码,其中包含看起来有点像这样的代码:

public bool Execute(MyArgument arg)
{
if(arg.Condition)
{
switch(arg.Data)
{
case DataValue.A:
case DataValue.B:
case DataValue.C:
return false;
case DataValue.F:
case DataValue.G:
case DataValue.H:
return true;
}
}
else
{
arg.DoSomeStuff();
return true;
}
}

我们使用的是 Given-When-Then 模式,我的问题是我应该用这样的测试来测试它:

Given NewContext When Execute with Condition True and DataValue A Then return false

Given NewContext When Execute with Condition True and DataValue B Then return false

Given NewContext When Execute with Condition True and DataValue C Then return false

Given NewContext When Execute with Condition True and DataValue D Then return true

Given NewContext When Execute with Condition True and DataValue E Then return true

Given NewContext When Execute with Condition True and DataValue F Then return true

Given NewContext When Execute with Condition False Then DoSomeStuff should be called Then return true

或者您有什么更好的方法吗? (如果您认为我的测试有效,请不要犹豫,在评论中说出来)。

最佳答案

我认为你得到的很好。就个人而言,我更喜欢冗长而不是更短但更不清晰的内容。至少您要测试的内容是显而易见的。

您也可以考虑利用 NUnit 中的 TestCase 属性。

[Test]
[TestCase(DataValue.A, false)]
[TestCase(DataValue.B, false)]
[TestCase(DataValue.C, false)]
[TestCase(DataValue.F, true)]
[TestCase(DataValue.G, true)]
[TestCase(DataValue.H, true)]
public void GetExpectedValueWhenConditionIsTrue(DataValue argData, bool expectedResult)
{
var c = new YourClass();
var arg = new MyArgument { Condition = true, Data = argData };

var actualResult = c.Execute(arg);

Assert.AreEqual(expectedResult, actualResult);
}

关于c# - 使用 Given-When-Then 模式的 switch-case 测试方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35958179/

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