public void ShouldDoNothingIfDifferentExceptionType()
 {
     ExceptionWrapper<InvalidOperationException> wrapper = new ExceptionWrapper<InvalidOperationException>((e, m) => e);
     IInvocation invocation = Substitute.For<IInvocation>();
     invocation.Request.Target.Returns(typeof(WithAttribute));
     invocation.Request.Method.Returns(typeof(WithAttribute).GetMethod("DoSomething"));
     invocation.When(i => i.Proceed()).Do(x => { throw new OutOfMemoryException(); });
     wrapper.Intercept(invocation);
 }
        public void ShouldDoNothingIfNoExceptionIsThrown()
        {
            ExceptionWrapper<Exception> wrapper = new ExceptionWrapper<Exception>((e, m) => e);

            IInvocation invocation = Substitute.For<IInvocation>();
            invocation.Request.Target.Returns(typeof(WithAttribute));
            invocation.Request.Method.Returns(typeof(WithAttribute).GetMethod("DoSomething"));
            wrapper.Intercept(invocation);
        }
예제 #3
0
        public void ShouldDoNothingIfDifferentExceptionType()
        {
            ExceptionWrapper <InvalidOperationException> wrapper = new ExceptionWrapper <InvalidOperationException>((e, m) => e);
            IInvocation invocation = Substitute.For <IInvocation>();

            invocation.Request.Target.Returns(typeof(WithAttribute));
            invocation.Request.Method.Returns(typeof(WithAttribute).GetMethod("DoSomething"));
            invocation.When(i => i.Proceed()).Do(x => { throw new OutOfMemoryException(); });
            wrapper.Intercept(invocation);
        }
예제 #4
0
        public void ShouldDoNothingIfNoExceptionIsThrown()
        {
            ExceptionWrapper <Exception> wrapper = new ExceptionWrapper <Exception>((e, m) => e);

            IInvocation invocation = Substitute.For <IInvocation>();

            invocation.Request.Target.Returns(typeof(WithAttribute));
            invocation.Request.Method.Returns(typeof(WithAttribute).GetMethod("DoSomething"));
            wrapper.Intercept(invocation);
        }
        public void ShouldUseClosestMatchWhenMultipleMessagesProvided()
        {
            ExceptionWrapper<Exception> wrapper = new ExceptionWrapper<Exception>((e, m) => new InvalidOperationException(m, e));

            IInvocation invocation = Substitute.For<IInvocation>();
            invocation.Request.Target.Returns(typeof(WithMultipleAttributes));
            invocation.Request.Method.Returns(typeof(WithMultipleAttributes).GetMethod("DoSomething"));
            const string parameterName = "x";
            invocation.When(i => i.Proceed()).Do(x => { throw new ArgumentNullException(parameterName); });
            try
            {
                wrapper.Intercept(invocation);
                Assert.Fail("The exception was swallowed.");
            }
            catch (InvalidOperationException exception)
            {
                Assert.AreEqual(WithMultipleAttributes.DerivedErrorMessage, exception.Message);
            }
        }
        public void ShouldIgnoreMessagesForMoreDerivedTypes()
        {
            ExceptionWrapper<Exception> wrapper = new ExceptionWrapper<Exception>((e, m) => new InvalidOperationException(m, e));

            IInvocation invocation = Substitute.For<IInvocation>();
            invocation.Request.Target.Returns(typeof(WithMultipleAttributes));
            invocation.Request.Method.Returns(typeof(WithMultipleAttributes).GetMethod("DoSomething"));
            const string message = "Error message";
            invocation.When(i => i.Proceed()).Do(x => { throw new ArgumentException(message); });
            try
            {
                wrapper.Intercept(invocation);
                Assert.Fail("The exception was swallowed.");
            }
            catch (InvalidOperationException exception)
            {
                Assert.AreEqual(WithMultipleAttributes.BaseErrorMessage, exception.Message);
            }
        }
예제 #7
0
        public void ShouldUseClosestMatchWhenMultipleMessagesProvided()
        {
            ExceptionWrapper <Exception> wrapper = new ExceptionWrapper <Exception>((e, m) => new InvalidOperationException(m, e));

            IInvocation invocation = Substitute.For <IInvocation>();

            invocation.Request.Target.Returns(typeof(WithMultipleAttributes));
            invocation.Request.Method.Returns(typeof(WithMultipleAttributes).GetMethod("DoSomething"));
            const string parameterName = "x";

            invocation.When(i => i.Proceed()).Do(x => { throw new ArgumentNullException(parameterName); });
            try
            {
                wrapper.Intercept(invocation);
                Assert.Fail("The exception was swallowed.");
            }
            catch (InvalidOperationException exception)
            {
                Assert.AreEqual(WithMultipleAttributes.DerivedErrorMessage, exception.Message);
            }
        }
예제 #8
0
        public void ShouldUseDifferentErrorMessageWithAttribute()
        {
            ExceptionWrapper <Exception> wrapper = new ExceptionWrapper <Exception>((e, m) => new InvalidOperationException(m, e));

            IInvocation invocation = Substitute.For <IInvocation>();

            invocation.Request.Target.Returns(typeof(WithAttribute));
            invocation.Request.Method.Returns(typeof(WithAttribute).GetMethod("DoSomething"));
            const string message = "Error message";

            invocation.When(i => i.Proceed()).Do(x => { throw new Exception(message); });
            try
            {
                wrapper.Intercept(invocation);
                Assert.Fail("The exception was swallowed.");
            }
            catch (InvalidOperationException exception)
            {
                Assert.AreEqual(WithAttribute.ErrorMessage, exception.Message);
            }
        }
        public void ShouldWrapExceptionAndUseItsMessageByDefault()
        {
            ExceptionWrapper<Exception> wrapper = new ExceptionWrapper<Exception>((e, m) => new InvalidOperationException(m, e));

            IInvocation invocation = Substitute.For<IInvocation>();
            invocation.Request.Target.Returns(typeof(NoAttribute));
            invocation.Request.Method.Returns(typeof(NoAttribute).GetMethod("DoSomething"));
            const string message = "Error message";
            invocation.When(i => i.Proceed()).Do(x => { throw new Exception(message); });
            try
            {
                wrapper.Intercept(invocation);
                Assert.Fail("The exception was swallowed.");
            }
            catch (InvalidOperationException exception)
            {
                Assert.AreEqual(message, exception.Message, "Did not use wrapped exception's message.");
            }
        }