コード例 #1
0
        public void Can_Show_Login_Page()
        {
            var mock = new Mock<IUserRepository>();
            mock.Setup(users => users.GetUsers()).Returns(new User[] { });

            var request = new FakeHttpRequest(null, null, null);
            request.SetIsAuthenticated(false);
            var httpContext = new Mock<HttpContextBase>();
            httpContext.Setup(x => x.Session).Returns(new FakeHttpSessionState(new System.Web.SessionState.SessionStateItemCollection()));
            httpContext.Setup(x => x.Request).Returns(request);
            var controllerContext = new Mock<ControllerContext>();
            controllerContext.Setup(c => c.HttpContext).Returns(httpContext.Object);

            var accountController = new AccountController(mock.Object);
            accountController.ControllerContext = controllerContext.Object;

            var result = accountController.Login() as ViewResult;

            Assert.IsNotNull(result);
        }
コード例 #2
0
        public void Dont_Show_Login_Page_When_User_Is_Authenticated()
        {
            Mock<IUserRepository> userMock = new Mock<IUserRepository>();
            userMock.Setup(users => users.GetUsers()).Returns(new User[] { });

            var request = new FakeHttpRequest(null, null, null);
            request.SetIsAuthenticated(true);
            var httpContext = new Mock<HttpContextBase>();
            httpContext.Setup(x => x.Session).Returns(new FakeHttpSessionState(new System.Web.SessionState.SessionStateItemCollection()));
            httpContext.Setup(x => x.Request).Returns(request);
            var controllerContext = new Mock<ControllerContext>();
            controllerContext.Setup(c => c.HttpContext).Returns(httpContext.Object);

            var accController = new AccountController(userMock.Object);
            accController.ControllerContext = controllerContext.Object;

            var result = accController.Login() as RedirectToRouteResult;

            Assert.IsNotNull(result);
            Assert.AreEqual("Index", result.RouteValues["action"]);
            Assert.AreEqual("Home", result.RouteValues["controller"]);
        }