public void TestBasicInterceptionExceptionThrow() { //Create and mock the interceptor. MockRepository mock = new MockRepository(); IAspect hmock = mock.CreateMock <IAspect>(); hmock.PreCall(null); LastCall.Return(MethodVoteOptions.Continue) .IgnoreArguments(); hmock.OnException(null, null, null); LastCall.Constraints( new Rhino.Mocks.Constraints.Anything(), Rhino.Mocks.Constraints.Is.TypeOf(typeof(NullReferenceException)), Rhino.Mocks.Constraints.Is.Anything()); mock.ReplayAll(); using (TestContainer container = new TestContainer(@"Concrete\Castle\AOP\TestSuite1\Configs\ConfigSample1.config")) { container.Kernel.RemoveComponent("interceptor"); container.Kernel.AddComponentInstance("interceptor", hmock); try { container.Resolve <ISomething>().OtherMethod(null, null); Assert.Fail("If reach here exception was not thrown"); } catch (Exception) { } } mock.VerifyAll(); }
public void TestIgnoreExeptionReturnValue() { //Create and mock the interceptor. MockRepository mock = new MockRepository(); IAspect hmock = mock.StrictMock <IAspect>(); Expect.Call(hmock.PreCall(null)) .Return(MethodVoteOptions.Continue) .IgnoreArguments(); Expect.Call(() => hmock.PostCall(null, null)) .IgnoreArguments(); Expect.Call(() => hmock.OnException(null, null, null)) .Callback(delegate(CastleIInvocation ii, Exception e, AspectExceptionAction action) { action.IgnoreException = true; return(true); }); mock.ReplayAll(); using (TestContainer container = new TestContainer(@"Concrete\Castle\AOP\TestSuite1\Configs\ConfigSample2.config")) { //Set the mock into the kernel. container.Kernel.RemoveComponent("interceptor"); container.Kernel.AddComponentInstance("interceptor", hmock); ISomething something = container.Resolve <ISomething>("throwclass"); String retvalue = something.OtherMethod("1", "2"); Assert.That(retvalue, Is.Null); //Default value is returned } mock.VerifyAll(); }
protected override object Invoke(MethodInfo targetMethod, object[] args) { object retval = null; args = _aspect.OnEnter(targetMethod, args); try { retval = targetMethod.Invoke(_service, args); } catch (TargetInvocationException ex) { _aspect.OnException(targetMethod, ex.InnerException); throw (ex.InnerException); } retval = _aspect.OnExit(targetMethod, args, retval); return(retval); }
public override void OnException(MethodInfo mi, Exception ex) { handler.OnException(mi, ex); }