gpt4 book ai didi

c# - 明确设置测试通过/失败?

转载 作者:太空狗 更新时间:2023-10-30 00:03:30 26 4
gpt4 key购买 nike

在下面的测试中,如果进入catch block 我想说明测试通过了。如果绕过 catch block ,我希望测试失败。

有没有办法做到这一点,或者我是否忽略了应该如何构建测试的要点?

[TestMethod]
public void CommandExecutionWillThrowExceptionIfUserDoesNotHaveEnoughEminence()
{
IUserCommand cmd = CreateDummyCommand("TEST", 10, 10);
IUser user = new User("chris", 40);

try
{
cmd.Execute(user);
}
catch(UserCannotExecuteCommandException e)
{
//Test Passed
}

// Test Failed
}

最佳答案

当我遇到类似情况时,我倾向于使用这种模式:

// ...
catch (UserCannotExecuteCommandException e)
{
return; // Test Passed
}

Assert.Fail(); // Test Failed -- expected exception not thrown

关于c# - 明确设置测试通过/失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8373333/

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