在 MSTest 中处理单元测试中的预期异常
Handle expected exception in unit test in MSTest
我正在用下面的测试方法测试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();
}
我正在用下面的测试方法测试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();
}