,你可以看到在输出异常的详细信息并不一定意味着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);
}
};