コード例 #1
0
        public async Task BindValuesAsync_Invokes_Inner_And_Traces()
        {
            // Arrange
            bool wasInvoked = false;
            Mock <HttpActionBinding> mockBinder = new Mock <HttpActionBinding>()
            {
                CallBase = true
            };

            mockBinder.Setup(b => b.ExecuteBindingAsync(It.IsAny <HttpActionContext>(), It.IsAny <CancellationToken>()))
            .Callback(() => wasInvoked = true).Returns(TaskHelpers.Completed());

            TestTraceWriter         traceWriter = new TestTraceWriter();
            HttpActionBindingTracer tracer      = new HttpActionBindingTracer(mockBinder.Object, traceWriter);

            TraceRecord[] expectedTraces = new TraceRecord[]
            {
                new TraceRecord(_actionContext.Request, TraceCategories.ModelBindingCategory, TraceLevel.Info)
                {
                    Kind = TraceKind.Begin
                },
                new TraceRecord(_actionContext.Request, TraceCategories.ModelBindingCategory, TraceLevel.Info)
                {
                    Kind = TraceKind.End
                }
            };

            // Act
            await tracer.ExecuteBindingAsync(_actionContext, CancellationToken.None);

            // Assert
            Assert.Equal <TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
            Assert.True(wasInvoked);
        }
コード例 #2
0
        public void BindValuesAsync_Invokes_Inner_And_Traces()
        {
            // Arrange
            bool wasInvoked = false;
            Mock<HttpActionBinding> mockBinder = new Mock<HttpActionBinding>() { CallBase = true };
            mockBinder.Setup(b => b.ExecuteBindingAsync(
                It.IsAny<HttpActionContext>(),
                It.IsAny<CancellationToken>())).
                    Callback(() => wasInvoked = true).Returns(TaskHelpers.Completed());

            TestTraceWriter traceWriter = new TestTraceWriter();
            HttpActionBindingTracer tracer = new HttpActionBindingTracer(mockBinder.Object, traceWriter);

            TraceRecord[] expectedTraces = new TraceRecord[]
            {
                new TraceRecord(_actionContext.Request, TraceCategories.ModelBindingCategory, TraceLevel.Info) { Kind = TraceKind.Begin },
                new TraceRecord(_actionContext.Request, TraceCategories.ModelBindingCategory, TraceLevel.Info) { Kind = TraceKind.End }
            };

            // Act
            tracer.ExecuteBindingAsync(_actionContext, CancellationToken.None).Wait();

            // Assert
            Assert.Equal<TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
            Assert.True(wasInvoked);
        }
コード例 #3
0
        public void ExecuteBindingAsync_Faults_And_Traces_When_Inner_Faults()
        {
            // Arrange
            InvalidOperationException     exception = new InvalidOperationException();
            TaskCompletionSource <object> tcs       = new TaskCompletionSource <object>();

            tcs.TrySetException(exception);
            Mock <HttpActionBinding> mockBinder = new Mock <HttpActionBinding>()
            {
                CallBase = true
            };

            mockBinder.Setup(b => b.ExecuteBindingAsync(
                                 It.IsAny <HttpActionContext>(),
                                 It.IsAny <CancellationToken>())).
            Returns(tcs.Task);

            TestTraceWriter         traceWriter = new TestTraceWriter();
            HttpActionBindingTracer tracer      = new HttpActionBindingTracer(mockBinder.Object, traceWriter);

            TraceRecord[] expectedTraces = new TraceRecord[]
            {
                new TraceRecord(_actionContext.Request, TraceCategories.ModelBindingCategory, TraceLevel.Info)
                {
                    Kind = TraceKind.Begin
                },
                new TraceRecord(_actionContext.Request, TraceCategories.ModelBindingCategory, TraceLevel.Error)
                {
                    Kind = TraceKind.End
                }
            };

            // Act
            Task task = tracer.ExecuteBindingAsync(_actionContext, CancellationToken.None);

            // Assert
            Exception thrown = Assert.Throws <InvalidOperationException>(() => task.Wait());

            Assert.Equal <TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
            Assert.Same(exception, thrown);
            Assert.Same(exception, traceWriter.Traces[1].Exception);
        }
コード例 #4
0
        public void ExecuteBindingAsync_Faults_And_Traces_When_Inner_Faults()
        {
            // Arrange
            InvalidOperationException exception = new InvalidOperationException();
            TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
            tcs.TrySetException(exception);
            Mock<HttpActionBinding> mockBinder = new Mock<HttpActionBinding>() { CallBase = true };
            mockBinder.Setup(b => b.ExecuteBindingAsync(It.IsAny<HttpActionContext>(), It.IsAny<CancellationToken>()))
                .Returns(tcs.Task);

            TestTraceWriter traceWriter = new TestTraceWriter();
            HttpActionBindingTracer tracer = new HttpActionBindingTracer(mockBinder.Object, traceWriter);

            TraceRecord[] expectedTraces = new TraceRecord[]
            {
                new TraceRecord(_actionContext.Request, TraceCategories.ModelBindingCategory, TraceLevel.Info) { Kind = TraceKind.Begin },
                new TraceRecord(_actionContext.Request, TraceCategories.ModelBindingCategory, TraceLevel.Error) { Kind = TraceKind.End }
            };

            // Act
            Task task = tracer.ExecuteBindingAsync(_actionContext, CancellationToken.None);

            // Assert
            Exception thrown = Assert.Throws<InvalidOperationException>(() => task.Wait());
            Assert.Equal<TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
            Assert.Same(exception, thrown);
            Assert.Same(exception, traceWriter.Traces[1].Exception);
        }