【发布时间】:2011-01-31 18:39:40
【问题描述】:
谁能告诉我为什么这个检查异常的单元测试会失败?显然,我真正的测试是检查其他代码,但我使用 Int32.Parse 来显示问题。
[Test]
public void MyTest()
{
Assert.That(Int32.Parse("abc"), Throws.Exception.TypeOf<FormatException>());
}
测试失败,出现此错误。显然我正在尝试测试这个异常,我认为我的语法中遗漏了一些东西。
Error 1 TestCase '.MyTest'
failed: System.FormatException : Input string was not in a correct format.
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.Int32.Parse(String s)
【问题讨论】:
-
您也可以这样做:
Assert.Throws<FormatException>(() => Int32.Parse("abc")); -
我试图在这个项目中坚持使用 Assert.That 风格。不过,我不像以前那样依恋它了。