Exemple #1
0
        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 TestMethodReturnPassedResult()
    {
        MethodInfo  method  = typeof(TestMethodCommandClass).GetMethod("TestMethod");
        TestCommand command = new FactCommand(Reflector.Wrap(method));

        MethodResult result = command.Execute(new TestMethodCommandClass());

        Assert.IsType <PassedResult>(result);
    }
    public void ExecuteRunsTest()
    {
        MethodInfo  method  = typeof(TestMethodCommandClass).GetMethod("TestMethod");
        TestCommand command = new FactCommand(Reflector.Wrap(method));

        TestMethodCommandClass.testCounter = 0;

        command.Execute(new TestMethodCommandClass());

        Assert.Equal(1, TestMethodCommandClass.testCounter);
    }
Exemple #4
0
        public void NameOnFactAttributeOverridesDisplayName()
        {
            MethodInfo    method = typeof(FactAttributeTests).GetMethod("CustomNamedFactMethod");
            FactAttribute attrib = method.GetCustomAttributes(true).OfType <FactAttribute>().Single();

            var commands = new List <ITestCommand>(attrib.CreateTestCommands(Reflector.Wrap(method)));

            ITestCommand command     = Assert.Single(commands);
            FactCommand  factCommand = Assert.IsType <FactCommand>(command);

            Assert.Equal("Custom display name", factCommand.DisplayName);
        }
    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 #6
0
        public void DefaultFactAttributeValues()
        {
            FactAttribute attrib = new FactAttribute();
            MethodInfo    method = typeof(FactAttributeTests).GetMethod("DummyFactMethod");

            var commands = new List <ITestCommand>(attrib.CreateTestCommands(Reflector.Wrap(method)));

            ITestCommand command     = Assert.Single(commands);
            FactCommand  factCommand = Assert.IsType <FactCommand>(command);

            Assert.Equal("Xunit1.FactAttributeTests", factCommand.TypeName);
            Assert.Equal("DummyFactMethod", factCommand.MethodName);
            Assert.Equal("Xunit1.FactAttributeTests.DummyFactMethod", factCommand.DisplayName);
        }
    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);
    }
Exemple #9
0
        public static bool TryParse(string s, out FactCommand command)
        {
            command = null;

            if (PrivateMessage.TryParse(s, out var message))
            {
                if (message.Text == "fact")
                {
                    command = new FactCommand();
                    return(true);
                }
            }

            return(false);
        }
    public void TurnsParameterCountMismatchExceptionIntoInvalidOperationException()
    {
        Mock <IMethodInfo> method = new Mock <IMethodInfo>();

        method.SetupGet(m => m.Name)
        .Returns("StubbyName");
        method.SetupGet(m => m.TypeName)
        .Returns("StubbyType");
        method.Setup(m => m.Invoke(It.IsAny <object>(), It.IsAny <object[]>()))
        .Throws <ParameterCountMismatchException>();
        TestCommand command = new FactCommand(method.Object);

        Exception ex = Record.Exception(() => command.Execute(new TestMethodCommandClass()));

        Assert.IsType <InvalidOperationException>(ex);
        Assert.Equal("Fact method StubbyType.StubbyName cannot have parameters", ex.Message);
    }