public static IEnumerable <ITestCommand> Make(ITestClassCommand classCommand, IMethodInfo method) { foreach (var testCommand in classCommand.EnumerateTestCommands(method)) { ITestCommand wrappedCommand = testCommand; // Timeout (if they have one) -> Capture -> Timed -> Lifetime (if we need an instance) -> BeforeAfter wrappedCommand = new BeforeAfterCommand(wrappedCommand, method.MethodInfo); if (testCommand.ShouldCreateInstance) { wrappedCommand = new LifetimeCommand(wrappedCommand, method); } wrappedCommand = new TimedCommand(wrappedCommand); wrappedCommand = new ExceptionCaptureCommand(wrappedCommand, method); if (wrappedCommand.Timeout > 0) { wrappedCommand = new TimeoutCommand(wrappedCommand, wrappedCommand.Timeout, method); } yield return(wrappedCommand); } }
public void ExceptionThrownWhenCreatingClassInstanceProperlyReported() { var method = typeof(LifetimeCommandTests.SpyWithConstructorThrow).GetMethod("PassedTest"); var wrappedMethod = Reflector.Wrap(method); var factCommand = new FactCommand(wrappedMethod); var command = new LifetimeCommand(factCommand, wrappedMethod); LifetimeCommandTests.SpyWithConstructorThrow.Reset(); var ex = Record.Exception(() => command.Execute(null)); // If you get a test failure here, then there's another missing instance of "throw;" where there // is a call to RethrowWithNoStackTraceLoss. Specifically, for this test, it's in LifetimeCommand.Execute // Again, it should look like: // // catch (TargetInvocationException ex) // { // ExceptionUtility.RethrowWithNoStackTraceLoss(ex.InnerException); // throw; // <---- New line // } if (ex == null || ex.GetType() != typeof(TargetInvocationException)) { throw new ExceptionNotBeingRethrownException("LifetimeCommand.Execute"); } }
public void CreatesNewInstanceWhenPassedNull() { StubCommand innerCommand = new StubCommand(); MethodInfo method = typeof(StubCommand).GetMethod("Execute"); LifetimeCommand command = new LifetimeCommand(innerCommand, Reflector.Wrap(method)); command.Execute(null); Assert.NotNull(innerCommand.TestClass); }
public void DoesNotCreateNewInstanceWhenPassedExistingInstance() { StubCommand innerCommand = new StubCommand(); MethodInfo method = typeof(StubCommand).GetMethod("Execute"); LifetimeCommand command = new LifetimeCommand(innerCommand, Reflector.Wrap(method)); object instance = new object(); command.Execute(instance); Assert.Same(instance, innerCommand.TestClass); }
public void ConstructorThrowsTargetInvocationExceptionIsUnwrappedAndRethrown() { MethodInfo method = typeof(SpyWithConstructorThrow).GetMethod("PassedTest"); IMethodInfo wrappedMethod = Reflector.Wrap(method); FactCommand factCommand = new FactCommand(wrappedMethod); LifetimeCommand command = new LifetimeCommand(factCommand, wrappedMethod); SpyWithConstructorThrow.Reset(); Exception ex = Record.Exception(() => command.Execute(null)); Assert.IsType <InvalidOperationException>(ex); }
public void DuringTestThrowsDisposeCalled() { MethodInfo method = typeof(SpyWithTestThrow).GetMethod("FailedTest"); IMethodInfo wrappedMethod = Reflector.Wrap(method); TestCommand testCommand = new FactCommand(wrappedMethod); LifetimeCommand command = new LifetimeCommand(testCommand, wrappedMethod); SpyWithTestThrow.Reset(); Record.Exception(() => command.Execute(new SpyWithTestThrow())); Assert.Equal(1, SpyWithTestThrow.ctorCalled); Assert.Equal(1, SpyWithTestThrow.testCalled); Assert.Equal(1, SpyWithTestThrow.disposeCalled); }
public void ConstructorThrowsTestNotCalledDisposeNotCalled() { MethodInfo method = typeof(SpyWithConstructorThrow).GetMethod("PassedTest"); IMethodInfo wrappedMethod = Reflector.Wrap(method); TestCommand testCommand = new FactCommand(wrappedMethod); LifetimeCommand command = new LifetimeCommand(testCommand, wrappedMethod); SpyWithConstructorThrow.Reset(); Record.Exception(() => command.Execute(null)); Assert.Equal(1, SpyWithConstructorThrow.ctorCalled); Assert.Equal(0, SpyWithConstructorThrow.testCalled); Assert.Equal(0, SpyWithConstructorThrow.disposeCalled); }
public static IEnumerable <ITestCommand> Make(ITestClassCommand classCommand, IMethodInfo method) { foreach (var testCommand in classCommand.EnumerateTestCommands(method)) { var wrappedCommand = testCommand; wrappedCommand = new BeforeAfterCommand(wrappedCommand, method.MethodInfo); if (testCommand.ShouldCreateInstance) { wrappedCommand = new LifetimeCommand(wrappedCommand, method); } wrappedCommand = new TimedCommand(wrappedCommand); wrappedCommand = new ExceptionInterceptingCommand(wrappedCommand, method); wrappedCommand = new ExceptionAndOutputCaptureCommand(wrappedCommand, method); // Note that we don't use a TimeoutCommand - we'll let the Silverlight framework handle that yield return(wrappedCommand); } }