Exemplo n.º 1
0
    public void Display_UsesTemplateName()
    {
        // Arrange
        var model = new SomeModel {
            SomeProperty = "ModelValue"
        };
        var viewEngine = new Mock <ICompositeViewEngine>(MockBehavior.Strict);

        viewEngine
        .Setup(v => v.GetView(/*executingFilePath*/ null, It.IsAny <string>(), /*isMainPage*/ false))
        .Returns(ViewEngineResult.NotFound(string.Empty, Enumerable.Empty <string>()));
        viewEngine
        .Setup(v => v.FindView(It.IsAny <ActionContext>(), "DisplayTemplates/SomeTemplate", /*isMainPage*/ false))
        .Returns(ViewEngineResult.Found("SomeView", Mock.Of <IView>()))
        .Verifiable();
        var helper = DefaultTemplatesUtilities.GetHtmlHelper(model, viewEngine.Object);

        // Act
        var displayResult = helper.Display(expression: "SomeProperty", templateName: "SomeTemplate");

        // Assert
        viewEngine.Verify();
    }
Exemplo n.º 2
0
    public void Execute_CallsFindView_WithExpectedPath_WhenViewNameIsSpecified(string viewName)
    {
        // Arrange
        var viewEngine = new Mock <ICompositeViewEngine>(MockBehavior.Strict);

        viewEngine
        .Setup(v => v.GetView(/*executingFilePath*/ null, viewName, /*isMainPage*/ false))
        .Returns(ViewEngineResult.Found(viewName, new Mock <IView>().Object))
        .Verifiable();
        var viewData         = new ViewDataDictionary(new EmptyModelMetadataProvider());
        var componentContext = GetViewComponentContext(new Mock <IView>().Object, viewData);
        var componentResult  = new ViewViewComponentResult
        {
            ViewEngine = viewEngine.Object,
            ViewData   = viewData,
            ViewName   = viewName,
            TempData   = _tempDataDictionary,
        };

        // Act & Assert
        componentResult.Execute(componentContext);
        viewEngine.Verify();
    }
Exemplo n.º 3
0
        private ViewEngineResult LocatePageFromViewLocations(
            ActionContext actionContext,
            string viewName,
            bool isMainPage)
        {
            var controllerName = GetNormalizedRouteValue(actionContext, ControllerKey);
            var areaName       = GetNormalizedRouteValue(actionContext, AreaKey);

            var fileProvider = _options.FileProvider ?? _hostingEnvironment.ContentRootFileProvider;

            var checkedLocations = new List <string>();

            foreach (var location in _options.ViewLocationFormats)
            {
                var view = string.Format(location, viewName, controllerName);
                if (fileProvider.GetFileInfo(view).Exists)
                {
                    return(ViewEngineResult.Found("Default", new FluidView(view, _fluidRendering)));
                }
                checkedLocations.Add(view);
            }
            return(ViewEngineResult.NotFound(viewName, checkedLocations));
        }
Exemplo n.º 4
0
    public void GetView_ReturnsView_WhenExactlyOneViewEngineIsRegisteredWhichReturnsAFoundResult(bool isMainPage)
    {
        // Arrange
        var viewName         = "test-view.cshtml";
        var expectedViewName = "~/" + viewName;
        var engine           = new Mock <IViewEngine>(MockBehavior.Strict);
        var view             = Mock.Of <IView>();

        engine
        .Setup(e => e.GetView("~/Index.html", viewName, isMainPage))
        .Returns(ViewEngineResult.Found(expectedViewName, view));
        var optionsAccessor = Options.Create(new MvcViewOptions());

        optionsAccessor.Value.ViewEngines.Add(engine.Object);
        var compositeViewEngine = new CompositeViewEngine(optionsAccessor);

        // Act
        var result = compositeViewEngine.GetView("~/Index.html", viewName, isMainPage);

        // Assert
        Assert.True(result.Success);
        Assert.Same(view, result.View);
    }
        public void Display_UsesTemplateFormatted(HtmlHelperDisplayExtensionsTest.FormatModel model, string expectedResult)
        {
            // Arrange
            var view = new Mock<IView>();
            view.Setup(v => v.RenderAsync(It.IsAny<ViewContext>()))
                .Callback((ViewContext v) => v.Writer.WriteAsync(v.ViewData.TemplateInfo.FormattedModelValue.ToString()))
                .Returns(Task.FromResult(0));
            var viewEngine = new Mock<ICompositeViewEngine>(MockBehavior.Strict);
            viewEngine
                .Setup(v => v.GetView(/*executingFilePath*/ null, It.IsAny<string>(), /*isMainPage*/ false))
                .Returns(ViewEngineResult.NotFound(string.Empty, Enumerable.Empty<string>()));
            viewEngine
                .Setup(v => v.FindView(It.IsAny<ActionContext>(), "EditorTemplates/Status", /*isMainPage*/ false))
                .Returns(ViewEngineResult.Found("Status", view.Object))
                .Verifiable();
            var helper = DefaultTemplatesUtilities.GetHtmlHelper(model, viewEngine.Object);

            // Act
            var displayResult = helper.EditorFor(x => x.FormatProperty);

            // Assert
            Assert.Equal(expectedResult, HtmlContentUtilities.HtmlContentToString(displayResult));
        }
Exemplo n.º 6
0
        public async Task Render_returns_email_string_created_by_view_retrievepath()
        {
            var viewEngine = new Mock <IRazorViewEngine>();
            var view       = new FakeView();

            viewEngine.Setup(e => e.FindView(It.IsAny <ActionContext>(), "~/Views/TestFolder/Test", It.IsAny <bool>()))
            .Returns(ViewEngineResult.NotFound("Test", new string[0]));
            viewEngine.Setup(e => e.GetView(It.IsAny <string>(), "~/Views/TestFolder/Test", It.IsAny <bool>()))
            .Returns(ViewEngineResult.Found("~/Views/TestFolder/Test", view)).Verifiable();

            var logger                       = new Mock <ILogger <TemplateService> >();
            var serviceProvider              = new Mock <IServiceProvider>();
            var tempDataProvider             = new Mock <ITempDataProvider>();
            var hostingEnvironment           = new Mock <IWebHostEnvironment>();
            ITemplateService templateService = new TemplateService(logger.Object, viewEngine.Object, serviceProvider.Object, tempDataProvider.Object, hostingEnvironment.Object);
            var renderer                     = new EmailViewRender(templateService);

            var actualEmailString = await renderer.RenderAsync(new Email("~/Views/TestFolder/Test"));

            actualEmailString.ShouldBe("Fake");

            viewEngine.Verify();
        }
Exemplo n.º 7
0
        public async Task ProcessAsync_DisposesViewInstance()
        {
            // Arrange
            var bufferScope = new TestViewBufferScope();
            var partialName = "_Partial";
            var viewContext = GetViewContext();

            var disposable = new Mock <IDisposable>();

            disposable.Setup(d => d.Dispose()).Verifiable();
            var view = disposable.As <IView>();

            view.Setup(v => v.RenderAsync(It.IsAny <ViewContext>()))
            .Returns(Task.CompletedTask)
            .Verifiable();

            var viewEngine = new Mock <ICompositeViewEngine>();

            viewEngine.Setup(v => v.GetView(It.IsAny <string>(), partialName, false))
            .Returns(ViewEngineResult.Found(partialName, view.Object));

            var tagHelper = new PartialTagHelper(viewEngine.Object, bufferScope)
            {
                Name        = partialName,
                ViewContext = viewContext,
            };
            var tagHelperContext = GetTagHelperContext();
            var output           = GetTagHelperOutput();

            // Act
            await tagHelper.ProcessAsync(tagHelperContext, output);

            // Assert
            disposable.Verify();
            view.Verify();
        }
Exemplo n.º 8
0
        public async Task ViewResult_UsesFindViewOnSpecifiedViewEngineToLocateViews()
        {
            // Arrange
            var viewName   = "myview";
            var context    = new ActionContext(new DefaultHttpContext(), new RouteData(), new ActionDescriptor());
            var viewEngine = new Mock <IViewEngine>();
            var view       = Mock.Of <IView>();

            viewEngine.Setup(e => e.FindView(context, "myview"))
            .Returns(ViewEngineResult.Found("myview", view))
            .Verifiable();

            var viewResult = new ViewResult
            {
                ViewName   = viewName,
                ViewEngine = viewEngine.Object
            };

            // Act
            await viewResult.ExecuteResultAsync(context);

            // Assert
            viewEngine.Verify();
        }
Exemplo n.º 9
0
    public void GetView_ReturnsViewFromFirstViewEngineWithFoundResult(bool isMainPage)
    {
        // Arrange
        var viewName         = "foo.cshtml";
        var expectedViewName = "~/" + viewName;
        var engine1          = new Mock <IViewEngine>(MockBehavior.Strict);
        var engine2          = new Mock <IViewEngine>(MockBehavior.Strict);
        var engine3          = new Mock <IViewEngine>(MockBehavior.Strict);
        var view2            = Mock.Of <IView>();
        var view3            = Mock.Of <IView>();

        engine1
        .Setup(e => e.GetView("~/Index.html", viewName, isMainPage))
        .Returns(ViewEngineResult.NotFound(expectedViewName, Enumerable.Empty <string>()));
        engine2
        .Setup(e => e.GetView("~/Index.html", viewName, isMainPage))
        .Returns(ViewEngineResult.Found(expectedViewName, view2));
        engine3
        .Setup(e => e.GetView("~/Index.html", viewName, isMainPage))
        .Returns(ViewEngineResult.Found(expectedViewName, view3));

        var optionsAccessor = Options.Create(new MvcViewOptions());

        optionsAccessor.Value.ViewEngines.Add(engine1.Object);
        optionsAccessor.Value.ViewEngines.Add(engine2.Object);
        optionsAccessor.Value.ViewEngines.Add(engine3.Object);
        var compositeViewEngine = new CompositeViewEngine(optionsAccessor);

        // Act
        var result = compositeViewEngine.GetView("~/Index.html", viewName, isMainPage);

        // Assert
        Assert.True(result.Success);
        Assert.Same(view2, result.View);
        Assert.Equal(expectedViewName, result.ViewName);
    }
Exemplo n.º 10
0
 public ViewEngineResult FindView(ActionContext context, string viewName, bool isMainPage)
 {
     return(ViewEngineResult.Found("Grid", new MyView()));
 }
Exemplo n.º 11
0
        public async Task RenderPartialViewToString_should_successfully_render_view_into_string()
        {
            // Arrange.

            string data            = "this is the data";
            string partialViewPath = "temp/view/myview";

            // Http context
            Mock <HttpContext>        mockContext        = new Mock <HttpContext>();
            Mock <IFeatureCollection> mockFeaturesColl   = new Mock <IFeatureCollection>();
            Mock <IRoutingFeature>    mockRoutingFeature = new Mock <IRoutingFeature>();
            Mock <RouteData>          mockRoutData       = new Mock <RouteData>();

            mockRoutingFeature.Setup(m => m.RouteData).Returns(mockRoutData.Object);
            mockFeaturesColl.Setup(m => m.Get <IRoutingFeature>()).Returns(mockRoutingFeature.Object);
            mockContext.Setup(m => m.Features).Returns(mockFeaturesColl.Object);
            mockHttpContextAccessor.Setup(m => m.HttpContext).Returns(mockContext.Object);

            // Razor view engine mocks
            Mock <IViewEngine> mockEngine = new Mock <IViewEngine>();
            Mock <IView>       mockView   = new Mock <IView>();

            mockRazorViewEngine.Setup(m => m.FindView(It.IsAny <ActionContext>(), It.IsAny <string>(), It.IsAny <bool>())).Returns(ViewEngineResult.Found(partialViewPath, mockView.Object));
            IViewRenderer renderer = new ViewRenderer(mockViewOptions.Object, mockRazorViewEngine.Object, mockTempDataProvider.Object, mockHttpContextAccessor.Object);

            // IOptions

            Mock <MvcViewOptions> mockMvcOptions = new Mock <MvcViewOptions>();
            MvcViewOptions        opt            = new MvcViewOptions();

            opt.HtmlHelperOptions = new HtmlHelperOptions();
            //mockMvcOptions.Setup(m => m.HtmlHelperOptions).Returns(new HtmlHelperOptions());
            mockViewOptions.Setup(m => m.Value).Returns(opt);

            // Act.

            string htmlString = await renderer.RenderPartialViewToString(data, partialViewPath);

            // Assert.

            // since we are not passing in any view ... the result html string should be empty.
            Assert.IsEmpty(htmlString);
        }
Exemplo n.º 12
0
 // Mock MVC ViewEngines
 void AddViewEngine()
 {
     view       = Pleasure.Mock <IView>();
     viewEngine = Pleasure.Mock <IViewEngine>();
     viewEngine.Setup(r => r.FindView(Pleasure.MockIt.IsAny <ControllerContext>(), Pleasure.MockIt.IsAny <string>(), Pleasure.MockIt.IsAny <bool>())).Returns(ViewEngineResult.Found(view.Object.Path, view.Object));
     //ViewEngines.Engines.Clear();
     //ViewEngines.Engines.Add(viewEngine.Object);
 }
Exemplo n.º 13
0
        public new ViewEngineResult GetView(string executingFilePath, string viewPath, bool isMainPage)
        {
            var result = base.GetView(executingFilePath, viewPath, isMainPage);

            return(result.Success ? ViewEngineResult.Found(result.ViewName, new DynamicFormsView(result.View)) : result);
        }
Exemplo n.º 14
0
        public new ViewEngineResult FindView(ActionContext context, string viewName, bool isMainPage)
        {
            var result = base.FindView(context, viewName, isMainPage);

            return(result.Success ? ViewEngineResult.Found(result.ViewName, new DynamicFormsView(result.View)) : result);
        }
Exemplo n.º 15
0
 public ViewEngineResult GetView(string executingFilePath, string viewPath, bool isMainPage)
 {
     return(ViewEngineResult.Found("vidok", new MainView()));
 }
Exemplo n.º 16
0
        public async Task ExecuteResultAsync_UsesProvidedViewEngine()
        {
            // Arrange
            var expected = new byte[] { 97, 98, 99, 100 };

            var view = new Mock <IView>();

            view.Setup(v => v.RenderAsync(It.IsAny <ViewContext>()))
            .Callback((ViewContext v) =>
            {
                view.ToString();
                v.Writer.Write("abcd");
            })
            .Returns(Task.FromResult(0));

            var routeDictionary = new Dictionary <string, object>();

            var goodViewEngine = new Mock <IViewEngine>();

            var badViewEngine = new Mock <ICompositeViewEngine>(MockBehavior.Strict);

            var serviceProvider = new Mock <IServiceProvider>();

            serviceProvider.Setup(sp => sp.GetService(typeof(ICompositeViewEngine)))
            .Returns(badViewEngine.Object);

            var memoryStream = new MemoryStream();
            var response     = new Mock <HttpResponse>();

            response.SetupGet(r => r.Body)
            .Returns(memoryStream);

            var context = new Mock <HttpContext>();

            context.SetupGet(c => c.Response)
            .Returns(response.Object);
            context.SetupGet(c => c.RequestServices)
            .Returns(serviceProvider.Object);

            var actionContext = new ActionContext(context.Object,
                                                  new RouteData()
            {
                Values = routeDictionary
            },
                                                  new ActionDescriptor());

            goodViewEngine.Setup(v => v.FindView(actionContext, It.IsAny <string>()))
            .Returns(ViewEngineResult.Found("MyView", view.Object));


            var viewResult = new ViewResult()
            {
                ViewEngine = goodViewEngine.Object,
            };

            // Act
            await viewResult.ExecuteResultAsync(actionContext);

            // Assert
            Assert.Equal(expected, memoryStream.ToArray());
        }