,你可以看到在輸出異常的詳細信息並不一定意味着NUnit的是注意到該異常。
我已經使用在AppDomain.UnhandledException
事件來監視這樣的情景測試(考慮到例外是未處理的,我以爲是這裏的情況):如果你只想測試特定的例外
bool exceptionWasThrown = false;
UnhandledExceptionEventHandler unhandledExceptionHandler = (s, e) =>
{
if (!exceptionWasThrown)
{
exceptionWasThrown = true;
}
};
AppDomain.CurrentDomain.UnhandledException += unhandledExceptionHandler;
// perform the test here, using whatever synchronization mechanisms needed
// to wait for threads to finish
// ...and detach the event handler
AppDomain.CurrentDomain.UnhandledException -= unhandledExceptionHandler;
// make assertions
Assert.IsFalse(exceptionWasThrown, "There was at least one unhandled exception");
你可以做的是,在事件處理程序:
UnhandledExceptionEventHandler unhandledExceptionHandler = (s, e) =>
{
if (!exceptionWasThrown)
{
exceptionWasThrown = e.ExceptionObject.GetType() ==
typeof(PassedSystem.ArgumentException);
}
};