コード例 #1
0
        public void TestLoggingIntercept()
        {
            const string methodName = "TestLoggedMethod";

            var loggingService = new TestLoggingService();
            var loggingAspect = new LoggingAspect(loggingService);

            var mockInvocation = new Mock<IInvocation>();
            mockInvocation.Setup(inv => inv.Method.Name).Returns(methodName);

            loggingAspect.Intercept(mockInvocation.Object);

            loggingService.Count.Should().Be(2);
            loggingService[0].Should().Be(methodName + " started");
            loggingService[1].Should().Be(methodName + " completed");
        }
コード例 #2
0
        public void TestLoggingInterceptMocks()
        {
            const string methodName = "TestLoggedMethod";

            var mockLoggingService = new Mock<ILoggingService>();
            mockLoggingService.Setup(ls => ls.Log(It.IsAny<string>()));

            var loggingService = mockLoggingService.Object;
            var loggingAspect = new LoggingAspect(loggingService);
            var mockInvocation = new Mock<IInvocation>();
            mockInvocation.Setup(inv => inv.Method.Name).Returns(methodName);

            loggingAspect.Intercept(mockInvocation.Object);

            mockLoggingService.Verify(ls => ls.Log(methodName + " started"));
            mockLoggingService.Verify(ls => ls.Log(methodName + " completed"));
        }