コード例 #1
0
        public void Index_MonthOutsideOfValidRange_RedisplaysViewWithSameViewModel([Values(-1, 13)]int month)
        {
            AgeGateViewModel model = new AgeGateViewModel
            {
                Day = 1,
                Month = month
            };

            AgeGateController controller = new AgeGateController();

            var result = controller.Index(model) as ViewResult;

            Assert.That(result != null);
            Assert.That(result.ViewName, Is.Empty.Or.EqualTo(nameof(AgeGateController.Index)));
            Assert.That(result.Model, Is.SameAs(model));
        }
コード例 #2
0
        public void Index_DayHigherThanNumberOfDaysInMonth_RedisplaysViewWithSameViewModel()
        {
            AgeGateViewModel model = new AgeGateViewModel
            {
                Day = 31, // Only 28 or 29 days
                Month = 2 // February
            };

            AgeGateController controller = new AgeGateController();

            var result = controller.Index(model) as ViewResult;

            Assert.That(result != null);
            Assert.That(result.ViewName, Is.Empty.Or.EqualTo(nameof(AgeGateController.Index)));
            Assert.That(result.Model, Is.SameAs(model));
        }
コード例 #3
0
        public void Index_LocalUrl_RedirectsToReturnUrl()
        {
            string returnUrl = "/Games/1";

            AgeGateViewModel model = new AgeGateViewModel
            {
                Day = 4,
                Month = 12,
                Year = 2015,
                ReturnUrl = returnUrl
            };

            HttpCookieCollection cookieCollection = new HttpCookieCollection();

            Mock<ControllerContext> contextStub = new Mock<ControllerContext>();
            contextStub.
                Setup(c => c.HttpContext.Response.Cookies).
                Returns(cookieCollection);

            Mock<UrlHelper> urlHelperStub = new Mock<UrlHelper>();
            urlHelperStub.
                Setup(uh => uh.IsLocalUrl(It.IsAny<string>())).
                Returns(true);

            AgeGateController controller = new AgeGateController
            {
                ControllerContext = contextStub.Object,
                Url = urlHelperStub.Object
            };

            var result = controller.Index(model) as RedirectResult;

            Assert.That(result != null);
            Assert.That(result.Url, Is.SameAs(returnUrl));
        }
コード例 #4
0
        public void Index_NonLocalUrl_RedirectsToHomeIndex()
        {
            AgeGateViewModel model = new AgeGateViewModel
            {
                Day = 4,
                Month = 12,
                Year = 2015
            };

            HttpCookieCollection cookieCollection = new HttpCookieCollection();

            Mock<ControllerContext> contextStub = new Mock<ControllerContext>();
            contextStub.
                Setup(c => c.HttpContext.Response.Cookies).
                Returns(cookieCollection);

            Mock<UrlHelper> urlHelperStub = new Mock<UrlHelper>();
            urlHelperStub.
                Setup(uh => uh.IsLocalUrl(It.IsAny<string>())).
                Returns(false);

            AgeGateController controller = new AgeGateController
            {
                ControllerContext = contextStub.Object,
                Url = urlHelperStub.Object
            };

            var result = controller.Index(model) as RedirectToRouteResult;

            Assert.That(result != null);
            Assert.That(result.RouteValues["Action"], Is.EqualTo(nameof(HomeController.Index)));
            Assert.That(result.RouteValues["Controller"], Is.EqualTo("Home"));
        }
コード例 #5
0
        public void Index_ValidDate_NewCookieExpiresInADay()
        {
            AgeGateViewModel model = new AgeGateViewModel
            {
                Day = 4,
                Month = 12,
                Year = 2015
            };

            HttpCookieCollection cookieCollection = new HttpCookieCollection();

            Mock<ControllerContext> contextStub = new Mock<ControllerContext>();
            contextStub.
                Setup(c => c.HttpContext.Response.Cookies).
                Returns(cookieCollection);

            Mock<UrlHelper> urlHelperStub = new Mock<UrlHelper>();
            urlHelperStub.
                Setup(uh => uh.IsLocalUrl(It.IsAny<string>())).
                Returns(false);

            AgeGateController controller = new AgeGateController
            {
                ControllerContext = contextStub.Object,
                Url = urlHelperStub.Object
            };

            controller.Index(model);

            HttpCookie cookie = cookieCollection[AgeGateController.DATE_OF_BIRTH_COOKIE];

            Assert.That(cookie != null);
            Assert.That(cookie.Expires, Is.EqualTo(DateTime.UtcNow.AddDays(1)).Within(1).Minutes);
        }
コード例 #6
0
        public void Index_ValidDate_AddsCookieWithCorrectKeyAndValue()
        {
            AgeGateViewModel model = new AgeGateViewModel
            {
                Day = 4,
                Month = 12,
                Year = 2015
            };

            HttpCookieCollection cookieCollection = new HttpCookieCollection();

            Mock<ControllerContext> contextStub = new Mock<ControllerContext>();
            contextStub.
                Setup(c => c.HttpContext.Response.Cookies).
                Returns(cookieCollection);

            Mock<UrlHelper> urlHelperStub = new Mock<UrlHelper>();
            urlHelperStub.
                Setup(uh => uh.IsLocalUrl(It.IsAny<string>())).
                Returns(false);

            AgeGateController controller = new AgeGateController
            {
                ControllerContext = contextStub.Object,
                Url = urlHelperStub.Object
            };

            controller.Index(model);

            HttpCookie cookie = cookieCollection[AgeGateController.DATE_OF_BIRTH_COOKIE];

            Assert.That(cookie != null);
            Assert.That(cookie.Name, Is.SameAs(AgeGateController.DATE_OF_BIRTH_COOKIE));
            Assert.That(cookie.Value, Is.EqualTo("12/4/2015"));
        }