Пример #1
0
        public void Index_Not_Authenticated_Return_View()
        {
            // Preparação
            var mockPrincipal = new Mock<IPrincipal>();
            mockPrincipal.SetupGet(p => p.Identity.Name).Returns("*****@*****.**");
            mockPrincipal.Setup(p => p.IsInRole("User")).Returns(true);
            mockPrincipal.Setup(p => p.Identity.IsAuthenticated).Returns(false);

            var mockContext = new Mock<ControllerContext>();
            mockContext.SetupGet(p => p.HttpContext.User).Returns(mockPrincipal.Object);
            mockContext.SetupGet(p => p.HttpContext.Request.IsAuthenticated).Returns(true);

            HomeController controller = new HomeController() { ControllerContext = mockContext.Object };

            // Teste
            ViewResult result = controller.Index() as ViewResult;

            // Assert
            Assert.IsNotNull(result);
        }
Пример #2
0
        public void Index_Authenticated_Return_Poll_Action()
        {
            // Preparação
            var mockPrincipal = new Mock<IPrincipal>();
            mockPrincipal.SetupGet(p => p.Identity.Name).Returns("*****@*****.**");
            mockPrincipal.Setup(p => p.IsInRole("User")).Returns(true);
            mockPrincipal.Setup(p => p.Identity.IsAuthenticated).Returns(true);

            var mockContext = new Mock<ControllerContext>();
            mockContext.SetupGet(p => p.HttpContext.User).Returns(mockPrincipal.Object);
            mockContext.SetupGet(p => p.HttpContext.Request.IsAuthenticated).Returns(true);

            HomeController controller = new HomeController() { ControllerContext = mockContext.Object };

            // Teste
            var result = controller.Index() as RedirectToRouteResult;

            // Verificações
            Assert.IsNotNull(result);
            Assert.AreEqual("Index", result.RouteValues["Action"]);
            Assert.AreEqual("Poll", result.RouteValues["Controller"]);
        }