TargetInvocationException
This is especially annoying if you have unit tests to excersise private methods of an assembly invoked from the test by reflection, you may be wanting to test a specific exception, here's a useful wrapper function to help out here:
public void ExpectedExceptionMethodInvokeWrapper(object[] invokeParams, object instance, MethodInfo methodToInvoke) { try { methodToInvoke.Invoke(instance, invokeParams); // Fail: Expected Exception of T } catch (TargetInvocationException tie) { if (tie.InnerException is T) { throw tie.InnerException; } else { // Fail: Expected Exception of T } } catch { // Fail: Expected TargetInvocationException, with valid inner exception } }
No comments:
Post a Comment