コード例 #1
0
        public void Should_Order_Aspects_From_Left_To_Right()
        {
            var aspectLog = new List<string>();
            var serviceLog = new List<string>();

            //Given
            var firstAspect = new FirstAspect();
            firstAspect.DoneSomething += (obj, ea) => aspectLog.Add(ea.Message);

            var secondAspect = new SecondAspect();
            secondAspect.DoneSomething += (obj, ea) => aspectLog.Add(ea.Message);

            var simpleService = new SimpleService();
            simpleService.DoneSomething += (obj, ea) => serviceLog.Add(ea.Message);
            var shouldNotBeCalledAspect = new Mock<IAspect>(MockBehavior.Strict);

            var advisor = new Advisor(JoinPointDefinition.AttributeBasedJoinPointDefinition, new List<IAspect>() { shouldNotBeCalledAspect.Object, secondAspect, shouldNotBeCalledAspect.Object, firstAspect });
            var myService = advisor.GetAdvicedProxy<ISimpleService>(simpleService);

            //Then
            var result = myService.DoMore("muchmore");

            shouldNotBeCalledAspect.VerifyAll();

            Assert.AreEqual(aspectLog.Count, 4);
            Assert.AreEqual("FirstAspectBefore", aspectLog[0]);
            Assert.AreEqual("SecondAspectBefore", aspectLog[1]);
            Assert.AreEqual("SecondAspectAfter", aspectLog[2]);
            Assert.AreEqual("FirstAspectAfter", aspectLog[3]);
        }
コード例 #2
0
        public void Should_Intercept_Only_Attributed_Methods()
        {
            var aspectLog = new List<string>();
            var serviceLog = new List<string>();

            //Given
            var aspect = new SimpleAspect();
            aspect.DoneSomething += (obj, ea) => aspectLog.Add(ea.Message);
            var simpleService = new SimpleService();
            simpleService.DoneSomething += (obj, ea) => serviceLog.Add(ea.Message);
            var shouldNotBeCalledAspect = new Mock<IAspect>(MockBehavior.Strict);

            var advisor = new Advisor(JoinPointDefinition.AttributeBasedJoinPointDefinition, new List<IAspect>() {aspect, shouldNotBeCalledAspect.Object});
            var myService = advisor.GetAdvicedProxy<ISimpleService>(simpleService);

            //Then
            var result = myService.DoSomething("hallo");

            //When
            Assert.AreEqual(aspectLog[0], "aspectCalled");
            Assert.AreEqual(aspectLog[1], "hallo");
            Assert.AreEqual(aspectLog[2], "DoneSomething");

            Assert.AreEqual(result, "executed");

            Assert.AreEqual(serviceLog[0], "hallo");

            shouldNotBeCalledAspect.VerifyAll();
        }