Exemple #1
0
        public void ViewComponent_Json_SetsResultData()
        {
            // Arrange
            var viewComponent = new TestViewComponent();
            var testData = new object();

            // Act
            var actualResult = viewComponent.Json(testData);

            // Assert
            Assert.IsType<JsonViewComponentResult>(actualResult);
            Assert.Same(testData, actualResult.Data);
        }
        public void ViewComponent_ViewData_StoresDataForViewBag()
        {
            // Arrange
            var viewComponent = new TestViewComponent();

            // Act
            viewComponent.ViewData["A"] = "Alice";
            viewComponent.ViewData["B"] = "Bob";

            // Assert
            Assert.Equal(2, viewComponent.ViewData.Count);
            Assert.Equal("Alice", viewComponent.ViewBag.A);
            Assert.Equal("Bob", viewComponent.ViewBag.B);
        }
Exemple #3
0
        public void ViewComponent_Content_SetsResultContentAndEncodedContent()
        {
            // Arrange
            var viewComponent = new TestViewComponent();
            var expectedContent = "TestContent&";
            var expectedEncodedContent = new HtmlString(HtmlEncoder.Default.Encode(expectedContent));

            // Act
            var actualResult = viewComponent.Content(expectedContent);

            // Assert
            Assert.IsType<ContentViewComponentResult>(actualResult);
            Assert.Same(expectedContent, actualResult.Content);
        }
        public void ViewComponent_ViewBag_UsesViewData()
        {
            // Arrange
            var viewComponent = new TestViewComponent();

            // Act
            viewComponent.ViewBag.A = "Alice";
            viewComponent.ViewBag.B = "Bob";

            // Assert
            Assert.Equal(2, viewComponent.ViewData.Count);
            Assert.Equal("Alice", viewComponent.ViewData["A"]);
            Assert.Equal("Bob", viewComponent.ViewData["B"]);
        }
        public void DefaultViewComponentActivator_ActivatesViewComponentContext()
        {
            // Arrange
            var activator = new DefaultViewComponentActivator();

            var context = new ViewComponentContext();
            var instance = new TestViewComponent();

            // Act
            activator.Activate(instance, context);

            // Assert
            Assert.Same(context, instance.ViewComponentContext);
        }
        public void ViewComponent_View_WithEmptyParameter_SetsResultViewWithDefaultViewName()
        {
            // Arrange
            var viewComponent = new TestViewComponent();

            // Act
            var actualResult = viewComponent.View();

            // Assert
            Assert.IsType<ViewViewComponentResult>(actualResult);
            Assert.NotSame(viewComponent.ViewData, actualResult.ViewData);
            Assert.Equal(new ViewDataDictionary<object>(viewComponent.ViewData), actualResult.ViewData);
            Assert.Null(actualResult.ViewData.Model);
            Assert.Null(actualResult.ViewName);
        }
Exemple #7
0
        public void ViewComponent_ViewContext_ViewData_ReturnsDefaultInstanceIfNull()
        {
            // Arrange && Act
            var viewComponent = new TestViewComponent();

            // Assert
            // ViewComponent.ViewContext returns the default instance for the unit test scenarios
            Assert.NotNull(viewComponent.ViewContext);
            Assert.NotNull(viewComponent.ViewContext.ViewData);

            // ViewComponent.ViewData returns the default instance for the unit test scenarios
            Assert.Empty(viewComponent.ViewContext.ViewData);
            Assert.NotNull(viewComponent.ViewData);
            Assert.Empty(viewComponent.ViewData);
            Assert.Same(viewComponent.ViewData, viewComponent.ViewContext.ViewData);
        }
Exemple #8
0
        public void ViewComponent_ViewData_StoresDataForViewBag()
        {
            // Arrange
            var viewComponent = new TestViewComponent()
            {
                ViewData = new ViewDataDictionary(metadataProvider: null),
            };

            // Act
            viewComponent.ViewData["A"] = "Alice";
            viewComponent.ViewData["B"] = "Bob";

            // Assert
            Assert.Equal(2, viewComponent.ViewData.Count);
            Assert.Equal("Alice", viewComponent.ViewBag.A);
            Assert.Equal("Bob", viewComponent.ViewBag.B);
        }
        public void ViewComponent_ViewBag_UsesViewData()
        {
            // Arrange
            var viewComponent = new TestViewComponent()
            {
                ViewData = new ViewDataDictionary(metadataProvider: new EmptyModelMetadataProvider()),
            };

            // Act
            viewComponent.ViewBag.A = "Alice";
            viewComponent.ViewBag.B = "Bob";

            // Assert
            Assert.Equal(2, viewComponent.ViewData.Count);
            Assert.Equal("Alice", viewComponent.ViewData["A"]);
            Assert.Equal("Bob", viewComponent.ViewData["B"]);
        }
Exemple #10
0
    public void ViewComponent_View_WithViewNameAndModelParameters_SetsResultViewWithCustomViewNameAndModel()
    {
        // Arrange
        var viewComponent = new TestViewComponent();

        var model = new object();

        // Act
        var actualResult = viewComponent.View("CustomViewName", model);

        // Assert
        Assert.IsType <ViewViewComponentResult>(actualResult);
        Assert.IsType <ViewDataDictionary <object> >(actualResult.ViewData);
        Assert.NotSame(viewComponent.ViewData, actualResult.ViewData);
        Assert.Equal(new ViewDataDictionary <object>(viewComponent.ViewData), actualResult.ViewData);
        Assert.Same(model, actualResult.ViewData.Model);
        Assert.Equal("CustomViewName", actualResult.ViewName);
    }
Exemple #11
0
        public void ViewComponent_View_WithEmptyParameter_SetsResultViewWithDefaultViewName()
        {
            // Arrange
            var viewComponent = new TestViewComponent()
            {
                ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider()),
            };

            // Act
            var actualResult = viewComponent.View();

            // Assert
            Assert.IsType <ViewViewComponentResult>(actualResult);
            Assert.NotSame(viewComponent.ViewData, actualResult.ViewData);
            Assert.Equal(new ViewDataDictionary <object>(viewComponent.ViewData), actualResult.ViewData);
            Assert.Null(actualResult.ViewData.Model);
            Assert.Null(actualResult.ViewName);
        }
Exemple #12
0
    public void ViewComponent_View_WithEmptyParameter_SetsViewDataModelWithDefaultViewName()
    {
        // Arrange
        var viewComponent = new TestViewComponent();
        var model         = new object();

        viewComponent.ViewData.Model = model;

        // Act
        var actualResult = viewComponent.View();

        // Assert
        Assert.IsType <ViewViewComponentResult>(actualResult);
        Assert.NotSame(viewComponent.ViewData, actualResult.ViewData);
        Assert.Equal(new ViewDataDictionary <object>(viewComponent.ViewData), actualResult.ViewData);
        Assert.Same(model, actualResult.ViewData.Model);
        Assert.Null(actualResult.ViewName);
    }
Exemple #13
0
    public void ViewComponent_View_WithViewNameAndNonObjectNullModelParameter_SetsResultViewWithViewNameAndNullModel()
    {
        // Arrange
        var viewComponent = new TestViewComponent();

        viewComponent.ViewData.Model = "Hello World!";

        // Act
        var actualResult = viewComponent.View <string>("CustomViewName", model: null);

        // Assert
        Assert.IsType <ViewViewComponentResult>(actualResult);
        Assert.IsType <ViewDataDictionary <string> >(actualResult.ViewData);
        Assert.NotSame(viewComponent.ViewData, actualResult.ViewData);
        Assert.Equal(new ViewDataDictionary <string>(viewComponent.ViewData), actualResult.ViewData);
        Assert.Null(actualResult.ViewData.Model);
        Assert.Equal("CustomViewName", actualResult.ViewName);
    }
Exemple #14
0
    public void ViewComponent_View_WithNullModelParameter_SetsResultViewWithDefaultViewNameAndNullModel()
    {
        // Arrange
        var viewComponent = new TestViewComponent();

        viewComponent.ViewData.Model = new object();
        object model = null;

        // Act
        var actualResult = viewComponent.View(model: model);

        // Assert
        Assert.IsType <ViewViewComponentResult>(actualResult);
        Assert.IsType <ViewDataDictionary <object> >(actualResult.ViewData);
        Assert.NotSame(viewComponent.ViewData, actualResult.ViewData);
        Assert.Equal(new ViewDataDictionary <object>(viewComponent.ViewData), actualResult.ViewData);
        Assert.Null(actualResult.ViewData.Model);
        Assert.Null(actualResult.ViewName);
    }
        public void DefaultViewComponentActivatorSetsAllPropertiesMarkedAsActivate()
        {
            // Arrange
            var activator       = new DefaultViewComponentActivator();
            var instance        = new TestViewComponent();
            var helper          = Mock.Of <IHtmlHelper <object> >();
            var serviceProvider = new Mock <IServiceProvider>();

            serviceProvider.Setup(p => p.GetService(typeof(IHtmlHelper <object>))).Returns(helper);
            var viewContext = GetViewContext(serviceProvider.Object);

            // Act
            activator.Activate(instance, viewContext);

            // Assert
            Assert.Same(helper, instance.Html);
            Assert.Same(viewContext, instance.ViewContext);
            Assert.IsType <ViewDataDictionary>(instance.ViewData);
        }
        public void DefaultViewComponentActivatorSetsAllPropertiesMarkedAsActivate()
        {
            // Arrange
            var activator = new DefaultViewComponentActivator();
            var instance = new TestViewComponent();
            var helper = Mock.Of<IHtmlHelper<object>>();
            var serviceProvider = new Mock<IServiceProvider>();
            serviceProvider.Setup(p => p.GetService(typeof(IHtmlHelper<object>))).Returns(helper);
            serviceProvider.Setup(p => p.GetService(typeof(ICompositeViewEngine))).Returns(Mock.Of<ICompositeViewEngine>());
            serviceProvider.Setup(p => p.GetService(typeof(IUrlHelper))).Returns(Mock.Of<IUrlHelper>());
            var viewContext = GetViewContext(serviceProvider.Object);

            // Act
            activator.Activate(instance, viewContext);

            // Assert
            Assert.Same(helper, instance.Html);
            Assert.Same(viewContext, instance.ViewContext);
            Assert.IsType<ViewDataDictionary>(instance.ViewData);
        }
        public void DefaultViewComponentActivator_ActivatesViewComponentContext()
        {
            // Arrange
            var expectedInstance = new TestViewComponent();

            var typeActivator = new Mock <ITypeActivatorCache>();

            typeActivator
            .Setup(ta => ta.CreateInstance <object>(It.IsAny <IServiceProvider>(), It.IsAny <Type>()))
            .Returns(expectedInstance);

            var activator = new DefaultViewComponentActivator(typeActivator.Object);

            var context = CreateContext(typeof(TestViewComponent));

            expectedInstance.ViewComponentContext = context;

            // Act
            var instance = activator.Create(context) as ViewComponent;

            // Assert
            Assert.NotNull(instance);
            Assert.Same(context, instance.ViewComponentContext);
        }
Exemple #18
0
        public void ViewComponent_View_WithViewNameAndModelParameters_SetsResultViewWithCustomViewNameAndModel()
        {
            // Arrange
            var viewComponent = new TestViewComponent()
            {
                ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider()),
            };
            var model = new object();

            // Act
            var actualResult = viewComponent.View("CustomViewName", model);

            // Assert
            Assert.IsType<ViewViewComponentResult>(actualResult);
            Assert.IsType<ViewDataDictionary<object>>(actualResult.ViewData);
            Assert.NotSame(viewComponent.ViewData, actualResult.ViewData);
            Assert.Equal(new ViewDataDictionary<object>(viewComponent.ViewData), actualResult.ViewData);
            Assert.Same(model, actualResult.ViewData.Model);
            Assert.Equal("CustomViewName", actualResult.ViewName);
        }