gpt4 book ai didi

c# - 在 MSTest 中处理单元测试中的预期异常

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

我正在使用以下测试方法测试 DoingSomething() 方法-

[TestMethod()]
[ExpectedException(typeof(ArgumentException),"Invalid currency.")]
public void ConvertCurrencyTest_ExhangeRate()
{
try
{
DoingSomething();
}
catch (ArgumentException Ex)
{
}
catch (Exception Ex)
{
Assert.Fail();
}
}

测试结果表明 DoingSomething() 没有抛出异常。但它确实引发了异常。

我在这里错过了什么?

最佳答案

您在 try/catch 中使用异常,因此它不会冒泡被测试捕获。

删除 try/catch 并让测试工具处理异常。任何其他异常都会自然导致测试失败。

[TestMethod()]
[ExpectedException(typeof(ArgumentException),"Invalid currency.")]
public void ConvertCurrencyTest_ExhangeRate() {
DoingSomething();
}

关于c# - 在 MSTest 中处理单元测试中的预期异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44807853/

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