Exemplo n.º 1
0
        public async Task ExecuteAsync_AsynchronouslyFlushesToTheResponseStream_PriorToDispose(int writeLength)
        {
            // Arrange
            var view = new Mock <IView>();

            view.Setup(v => v.RenderAsync(It.IsAny <ViewContext>()))
            .Returns((ViewContext v) =>
                     Task.Run(async() =>
            {
                var text = new string('a', writeLength);
                await v.Writer.WriteAsync(text);
            }));

            var context = new DefaultHttpContext();
            var stream  = new Mock <Stream>();

            context.Response.Body = stream.Object;

            var actionContext = new ActionContext(
                context,
                new RouteData(),
                new ActionDescriptor());
            var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider());

            // Act
            await ViewExecutor.ExecuteAsync(
                view.Object,
                actionContext,
                viewData,
                Mock.Of <ITempDataDictionary>(),
                new HtmlHelperOptions(),
                ViewExecutor.DefaultContentType);

            // Assert
            stream.Verify(s => s.FlushAsync(It.IsAny <CancellationToken>()), Times.Once());
            stream.Verify(s => s.Write(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>()), Times.Never());
        }
Exemplo n.º 2
0
        public async Task ExecuteAsync_DoesNotWriteToResponse_OnceExceptionIsThrown()
        {
            // Arrange
            var expectedLength = 0;

            var view = new Mock <IView>();

            view.Setup(v => v.RenderAsync(It.IsAny <ViewContext>()))
            .Callback((ViewContext v) =>
            {
                throw new Exception();
            });

            var context      = new DefaultHttpContext();
            var memoryStream = new MemoryStream();

            context.Response.Body = memoryStream;

            var actionContext = new ActionContext(
                context,
                new RouteData(),
                new ActionDescriptor());
            var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider());

            // Act
            await Record.ExceptionAsync(
                () => ViewExecutor.ExecuteAsync(
                    view.Object,
                    actionContext,
                    viewData,
                    null,
                    new HtmlHelperOptions(),
                    contentType: null));

            // Assert
            Assert.Equal(expectedLength, memoryStream.Length);
        }