【发布时间】:2013-10-01 04:39:56
【问题描述】:
我正在尝试使用C# UnitTest 中的ExpectedException 属性,但我无法让它与我的特定Exception 一起使用。这是我得到的:
注意:我在给我带来麻烦的行周围加上了星号。
[ExpectedException(typeof(Exception))]
public void TestSetCellContentsTwo()
{
// Create a new Spreadsheet instance for this test:
SpreadSheet = new Spreadsheet();
// If name is null then an InvalidNameException should be thrown. Assert that the correct
// exception was thrown.
ReturnVal = SpreadSheet.SetCellContents(null, "String Text");
**Assert.IsTrue(ReturnVal is InvalidNameException);**
// If text is null then an ArgumentNullException should be thrown. Assert that the correct
// exception was thrown.
ReturnVal = SpreadSheet.SetCellContents("A1", (String) null);
Assert.IsTrue(ReturnVal is ArgumentNullException);
// If name is invalid then an InvalidNameException should be thrown. Assert that the correct
// exception was thrown.
{
ReturnVal = SpreadSheet.SetCellContents("25", "String Text");
Assert.IsTrue(ReturnVal is InvalidNameException);
ReturnVal = SpreadSheet.SetCellContents("2x", "String Text");
Assert.IsTrue(ReturnVal is InvalidNameException);
ReturnVal = SpreadSheet.SetCellContents("&", "String Text");
Assert.IsTrue(ReturnVal is InvalidNameException);
}
}
我有 ExpectedException 捕获基本类型 Exception。这不应该管吗?我试过使用AttributeUsage,但也没有用。我知道我可以将它包装在 try/catch 块中,但我想看看我是否能弄清楚这种风格。
谢谢大家!
【问题讨论】:
标签: c# unit-testing exception expected-exception