public void CorrectHandlerUsed() { MyThrowsHandler th = new MyThrowsHandler(); ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th); HttpException ex = new HttpException(); IMethodInvocation mi = A.Fake <IMethodInvocation>(); A.CallTo(() => mi.Method).Returns(ReflectionUtils.GetMethod(typeof(object), "HashCode", new Type[] {})); A.CallTo(() => mi.Arguments).Returns(null); A.CallTo(() => mi.This).Returns(new object()); A.CallTo(() => mi.Proceed()).Throws(ex); try { ti.Invoke(mi); Assert.Fail(); } catch (Exception caught) { Assert.AreEqual(ex, caught); } Assert.AreEqual(1, th.GetCalls()); Assert.AreEqual(1, th.GetCalls("HttpException")); }
public void NestedInnerExceptionsAreNotPickedUp() { MyThrowsHandler throwsHandler = new MyThrowsHandler(); ThrowsAdviceInterceptor throwsInterceptor = new ThrowsAdviceInterceptor(throwsHandler); // nest the exceptions; make sure the advice gets applied because of the inner exception... Exception exception = new FormatException("Parent", new HttpException("Inner")); IMethodInvocation invocation = A.Fake <IMethodInvocation>(); A.CallTo(() => invocation.Proceed()).Throws(exception); try { throwsInterceptor.Invoke(invocation); Assert.Fail("Must have failed (by throwing an exception by this point - check the mock)."); } catch (Exception caught) { Assert.AreEqual(exception, caught); } Assert.AreEqual(0, throwsHandler.GetCalls(), "Must NOT have been handled, 'cos the HttpException was wrapped by " + "another Exception that did not have a handler."); Assert.AreEqual(0, throwsHandler.GetCalls("HttpException"), "Similarly, must NOT have been handled, 'cos the HttpException was wrapped by " + "another Exception that did not have a handler."); }
public void CorrectHandlerUsedForSubclass() { MyThrowsHandler th = new MyThrowsHandler(); ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th); // Extends RemotingException RemotingTimeoutException ex = new RemotingTimeoutException(); MockRepository repository = new MockRepository(); IMethodInvocation mi = (IMethodInvocation)repository.CreateMock(typeof(IMethodInvocation)); Expect.Call(mi.Proceed()).Throw(ex); repository.ReplayAll(); try { ti.Invoke(mi); Assert.Fail(); } catch (Exception caught) { Assert.AreEqual(ex, caught); } Assert.AreEqual(1, th.GetCalls()); Assert.AreEqual(1, th.GetCalls("RemotingException")); repository.VerifyAll(); }
public void CorrectHandlerUsed() { MyThrowsHandler th = new MyThrowsHandler(); ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th); HttpException ex = new HttpException(); MockRepository repository = new MockRepository(); IMethodInvocation mi = (IMethodInvocation)repository.CreateMock(typeof(IMethodInvocation)); Expect.Call(mi.Method).Return(ReflectionUtils.GetMethod(typeof(object), "HashCode", new Type[] {})); Expect.Call(mi.Arguments).Return(null); Expect.Call(mi.This).Return(new object()); Expect.Call(mi.Proceed()).Throw(ex); repository.ReplayAll(); try { ti.Invoke(mi); Assert.Fail(); } catch (Exception caught) { Assert.AreEqual(ex, caught); } Assert.AreEqual(1, th.GetCalls()); Assert.AreEqual(1, th.GetCalls("HttpException")); repository.VerifyAll(); }
public void NoHandlerMethodForThrowable() { MyThrowsHandler th = new MyThrowsHandler(); ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th); Assert.AreEqual(2, ti.HandlerMethodCount); Exception ex = new Exception(); MockRepository repository = new MockRepository(); IMethodInvocation mi = (IMethodInvocation)repository.CreateMock(typeof(IMethodInvocation)); Expect.Call(mi.Proceed()).Throw(ex); repository.ReplayAll(); try { ti.Invoke(mi); Assert.Fail(); } catch (Exception caught) { Assert.AreEqual(ex, caught); } Assert.AreEqual(0, th.GetCalls()); repository.VerifyAll(); }
public void NotInvoked() { IMethodInvocation mi = A.Fake <IMethodInvocation>(); MyThrowsHandler th = new MyThrowsHandler(); ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th); object ret = new object(); A.CallTo(() => mi.Proceed()).Returns(ret); Assert.AreEqual(ret, ti.Invoke(mi)); Assert.AreEqual(0, th.GetCalls()); }
public void CorrectHandlerUsedForSubclass() { MyThrowsHandler th = new MyThrowsHandler(); ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th); // Extends RemotingException RemotingTimeoutException ex = new RemotingTimeoutException(); IMethodInvocation mi = A.Fake <IMethodInvocation>(); A.CallTo(() => mi.Proceed()).Throws(ex); try { ti.Invoke(mi); Assert.Fail(); } catch (Exception caught) { Assert.AreEqual(ex, caught); } Assert.AreEqual(1, th.GetCalls()); Assert.AreEqual(1, th.GetCalls("RemotingException")); }
public void NotInvoked() { MockRepository repository = new MockRepository(); IMethodInvocation mi = (IMethodInvocation)repository.CreateMock(typeof(IMethodInvocation)); MyThrowsHandler th = new MyThrowsHandler(); ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th); object ret = new object(); Expect.Call(mi.Proceed()).Return(ret); repository.ReplayAll(); Assert.AreEqual(ret, ti.Invoke(mi)); Assert.AreEqual(0, th.GetCalls()); repository.VerifyAll(); }
public void NotInvoked() { MockRepository repository = new MockRepository(); IMethodInvocation mi = (IMethodInvocation) repository.CreateMock(typeof (IMethodInvocation)); MyThrowsHandler th = new MyThrowsHandler(); ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th); object ret = new object(); Expect.Call(mi.Proceed()).Return(ret); repository.ReplayAll(); Assert.AreEqual(ret, ti.Invoke(mi)); Assert.AreEqual(0, th.GetCalls()); repository.VerifyAll(); }
public void NoHandlerMethodForThrowable() { MyThrowsHandler th = new MyThrowsHandler(); ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th); Assert.AreEqual(2, ti.HandlerMethodCount); Exception ex = new Exception(); IMethodInvocation mi = A.Fake <IMethodInvocation>(); A.CallTo(() => mi.Proceed()).Throws(ex); try { ti.Invoke(mi); Assert.Fail(); } catch (Exception caught) { Assert.AreEqual(ex, caught); } Assert.AreEqual(0, th.GetCalls()); }
public void NestedInnerExceptionsAreNotPickedUp() { MyThrowsHandler throwsHandler = new MyThrowsHandler(); ThrowsAdviceInterceptor throwsInterceptor = new ThrowsAdviceInterceptor(throwsHandler); // nest the exceptions; make sure the advice gets applied because of the inner exception... Exception exception = new FormatException("Parent", new HttpException("Inner")); MockRepository repository = new MockRepository(); IMethodInvocation invocation = (IMethodInvocation)repository.CreateMock(typeof(IMethodInvocation)); Expect.Call(invocation.Proceed()).Throw(exception); repository.ReplayAll(); try { throwsInterceptor.Invoke(invocation); Assert.Fail("Must have failed (by throwing an exception by this point - check the mock)."); } catch (Exception caught) { Assert.AreEqual(exception, caught); } Assert.AreEqual(0, throwsHandler.GetCalls(), "Must NOT have been handled, 'cos the HttpException was wrapped by " + "another Exception that did not have a handler."); Assert.AreEqual(0, throwsHandler.GetCalls("HttpException"), "Similarly, must NOT have been handled, 'cos the HttpException was wrapped by " + "another Exception that did not have a handler."); repository.VerifyAll(); }
public void CorrectHandlerUsed() { MyThrowsHandler th = new MyThrowsHandler(); ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th); HttpException ex = new HttpException(); MockRepository repository = new MockRepository(); IMethodInvocation mi = (IMethodInvocation)repository.CreateMock(typeof(IMethodInvocation)); Expect.Call(mi.Method).Return(ReflectionUtils.GetMethod(typeof (object), "HashCode", new Type[] {})); Expect.Call(mi.Arguments).Return(null); Expect.Call(mi.This).Return(new object()); Expect.Call(mi.Proceed()).Throw(ex); repository.ReplayAll(); try { ti.Invoke(mi); Assert.Fail(); } catch (Exception caught) { Assert.AreEqual(ex, caught); } Assert.AreEqual(1, th.GetCalls()); Assert.AreEqual(1, th.GetCalls("HttpException")); repository.VerifyAll(); }