public void ReflectorInvokeThrowsParameterCountException() { var method = typeof(ReflectorTests.Invoke.TestMethodCommandClass).GetMethod("ThrowsException"); var wrappedMethod = Reflector.Wrap(method); var obj = new ReflectorTests.Invoke.TestMethodCommandClass(); var ex = Record.Exception(() => wrappedMethod.Invoke(obj, "Hello world")); // Windows Phone 7 appears to throw ArgumentException instead of the expected TargetParameterCountException // Simply add another catch clause into Reflector+ReflectionMethodInfo.Invoke for ArgumentException // try // { // method.Invoke(testClass, new object[0]); // } // #if WINDOWS_PHONE // catch (ArgumentException) // <--- Add this // { // throw new ParamterCountMismatchException(); // } // #endif // catch (TargetParameterCountException) // { // throw new ParamterCountMismatchException(); // } // catch (TargetInvocationException ex) // { // ExceptionUtility.RethrowWithNoStackTraceLoss(ex.InnerException); // throw; // } Assert.IsType <ParamterCountMismatchException>(ex); }
public void ExceptionThrownWhenInvokingMethodIsStillThrownAfterCommentingOutRethrowWithNoStackLoss() { var method = typeof(ReflectorTests.Invoke.TestMethodCommandClass).GetMethod("ThrowsException"); var wrappedMethod = Reflector.Wrap(method); var obj = new ReflectorTests.Invoke.TestMethodCommandClass(); Exception ex = Record.Exception(() => wrappedMethod.Invoke(obj)); if (ex == null) { // If you see this message, it means that exceptions aren't getting propogated out of the // unit test system. If you've commented out RethrowWithNoStackTraceLoss above, then you need // to add a "throw;" after where Rethrow... was being called - for this test, that means // Reflector.ReflectionMethodInfo.Invoke. It should look like: // // catch (TargetInvocationException ex) // { // ExceptionUtility.RethrowWithNoStackTraceLoss(ex.InnerException); // throw; // <---- New line // } // // This is the only test that displays a message box - if this test fails, it means that // the system isn't reporting failures, so we need to resort to something drastic to get // it working. Once this test works, other failures will be reported accurately. // Arguably, this could be split into two tests - one that the xunitcontrib provider is // properly reporting failures, and this method to assert that the correct changes are // applied to xunit var thisMethod = new StackTrace().GetFrame(0).GetMethod(); MessageBox.Show(string.Format("Failing tests are not being reported{0}{0}TargetInvocationException not being rethrown{0}See {1}.{2} for more details", Environment.NewLine, thisMethod.DeclaringType.Name, thisMethod.Name)); throw new ExceptionNotBeingRethrownException("Reflector.ReflectionMethodInfo.Invoke"); } Assert.NotNull(ex); Assert.IsType <TargetInvocationException>(ex); Assert.IsType <InvalidOperationException>(ex.InnerException); }