/// <summary>
        /// Make instances of <see cref="ITestCommand"/> objects for the given class and method.
        /// </summary>
        /// <param name="classCommand">The class command</param>
        /// <param name="method">The method under test</param>
        /// <returns>The set of <see cref="ITestCommand"/> objects</returns>
        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 ExceptionAndOutputCaptureCommand(wrappedCommand, method);
                wrappedCommand = new TimedCommand(wrappedCommand);

                if (wrappedCommand.Timeout > 0)
                {
                    wrappedCommand = new TimeoutCommand(wrappedCommand, wrappedCommand.Timeout, method);
                }

                yield return(wrappedCommand);
            }
        }
Exemple #2
0
        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);
        }
Exemple #3
0
        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);
        }
Exemple #4
0
        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);
        }
Exemple #5
0
        public void DisposeThrowsTestCalled()
        {
            MethodInfo method = typeof(SpyWithDisposeThrow).GetMethod("PassedTest");
            IMethodInfo wrappedMethod = Reflector.Wrap(method);
            TestCommand testCommand = new FactCommand(wrappedMethod);
            LifetimeCommand command = new LifetimeCommand(testCommand, wrappedMethod);
            SpyWithDisposeThrow.Reset();

            Record.Exception(() => command.Execute(new SpyWithDisposeThrow()));

            Assert.Equal(1, SpyWithDisposeThrow.ctorCalled);
            Assert.Equal(1, SpyWithDisposeThrow.testCalled);
            Assert.Equal(1, SpyWithDisposeThrow.disposeCalled);
        }
        /// <summary>
        /// Make instances of <see cref="ITestCommand"/> objects for the given class and method.
        /// </summary>
        /// <param name="classCommand">The class command</param>
        /// <param name="method">The method under test</param>
        /// <returns>The set of <see cref="ITestCommand"/> objects</returns>
        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 ExceptionAndOutputCaptureCommand(wrappedCommand, method);

                if (wrappedCommand.Timeout > 0)
                    wrappedCommand = new TimeoutCommand(wrappedCommand, wrappedCommand.Timeout, method);

                yield return wrappedCommand;
            }
        }