Ejemplo n.º 1
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 = new TestOptionsManager <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);
        }
Ejemplo n.º 2
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);
        }