Exemplo n.º 1
0
        public void ViewBannedUsersTest()
        {
            Mock <ControllerContext> controllerContext = new Mock <ControllerContext>();
            Mock <IPrincipal>        principal         = new Mock <IPrincipal>();

            principal.SetupGet(x => x.Identity.Name).Returns("user");
            controllerContext.SetupGet(x => x.HttpContext.User).Returns(principal.Object);

            AppUser user1 = new AppUser
            {
                Email     = "*****@*****.**",
                Id        = "user1",
                FirstName = "John",
                LastName  = "Snow",
                IsBanned  = false,
                UserName  = "******"
            };
            AppUser user2 = new AppUser
            {
                Email     = "*****@*****.**",
                Id        = "user2",
                FirstName = "Ned",
                LastName  = "Stark",
                IsBanned  = true,
                UserName  = "******"
            };

            Mock <IMainRepository> repository = new Mock <IMainRepository>();

            repository.Setup(x => x.GetUsers()).Returns(new List <AppUser>
            {
                user1, user2
            }
                                                        );


            BannedUsersController controller = new BannedUsersController(repository.Object)
            {
                ControllerContext = controllerContext.Object
            };

            var result = (BannedUsersModel)controller.ViewBannedUsers().ViewData.Model;

            Assert.AreEqual("user2", result.BannedUsers.ToList()[0].Id);
            Assert.AreEqual("Ned", result.BannedUsers.ToList()[0].FirstName);
            Assert.AreEqual("Stark", result.BannedUsers.ToList()[0].LastName);
            Assert.AreEqual(true, result.BannedUsers.ToList()[0].IsBanned);
            Assert.AreEqual("Ned", result.BannedUsers.ToList()[0].UserName);
        }