Exemplo n.º 1
0
        public void TestLastVisitNotUpdated()
        {
            //Prepare
            var lastVisitHelperMock = new Mock <ILastVisitHelper>();

            lastVisitHelperMock.Setup(h => h.SaveUserLastVisit(It.IsAny <string>(), It.IsAny <HttpRequestBase>()));

            var filter = new LastVisitFilter(lastVisitHelperMock.Object);

            var fakeHttpContext  = new Mock <HttpContextBase>();
            var fakeIdentityMock = new Mock <IIdentity>();

            fakeIdentityMock.Setup(i => i.IsAuthenticated).Returns(false);
            fakeHttpContext.SetupGet(t => t.User.Identity).Returns(fakeIdentityMock.Object);
            var controllerContext = new Mock <ControllerContext>();

            controllerContext.Setup(t => t.HttpContext).Returns(fakeHttpContext.Object);
            var filterContext = new ActionExecutingContext {
                HttpContext = fakeHttpContext.Object
            };

            //Act
            filter.OnActionExecuting(filterContext);

            //Assert
            lastVisitHelperMock.Verify(h => h.SaveUserLastVisit(It.IsAny <string>(), It.IsAny <HttpRequestBase>()), Times.Never);
        }
Exemplo n.º 2
0
        public void TestLastVisitUpdated()
        {
            var user = new AspNetUser()
            {
                UserName = "******"
            };
            var filter           = new LastVisitFilter(new LastVisitHelper());
            var filterContext    = new ActionExecutingContext();
            var fakeHttpContext  = new Mock <HttpContextBase>();
            var fakeIdentityMock = new Mock <IIdentity>();

            fakeIdentityMock.Setup(i => i.IsAuthenticated).Returns(true);
            fakeIdentityMock.Setup(i => i.Name).Returns(user.UserName);
            fakeIdentityMock.SetupGet(i => i.Name).Returns(user.UserName);
            fakeHttpContext.SetupGet(t => t.User.Identity).Returns(fakeIdentityMock.Object);
            var controllerContext = new Mock <ControllerContext>();

            controllerContext.Setup(t => t.HttpContext).Returns(fakeHttpContext.Object);
            filterContext.HttpContext = fakeHttpContext.Object;
            DateTime before;
            DateTime after;

            using (HellolingoEntities db = new HellolingoEntities())
            {
                User userBefore = db.Users.FirstOrDefault(u => u.AspNetUser.UserName == user.UserName);
                before = userBefore.LastVisit.Value;
            }

            filter.OnActionExecuting(filterContext);
            using (HellolingoEntities db = new HellolingoEntities())
            {
                User userAfter = db.Users.FirstOrDefault(u => u.AspNetUser.UserName == user.UserName);
                after = userAfter.LastVisit.Value;
            }
            Assert.IsTrue(after > before);
        }