我正在使用以下测试方法测试 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/