示例#1
0
        public RolesInfoViewModel GetAllRoles()
        {
            RolesInfoViewModel ol = new RolesInfoViewModel();

            try
            {
                ol.Roles = new MyUserManager().GetAllRoles(); //await UserManager.CreateAsync(user, model.Password);
            }
            catch (Exception ex) { ModelState.AddModelError("Unable to fetch roles.", ex.Message); }
            if (ol.Roles.Count <= 0)
            {
                ModelState.AddModelError("", "No role exists.");
            }

            return(ol);
        }
示例#2
0
        //列表动作
        public ViewResult List(int page = 1)
        {
            RolesInfoViewModel viewModel = new RolesInfoViewModel
            {
                RolesInfo = repository.RolesInfo
                            .OrderBy(p => p.RoleName)
                            .Skip((page - 1) * PageSize)
                            .Take(PageSize),
                pagingInfo = new PagingInfo
                {
                    CurrentPage  = page,
                    ItemsPerPage = PageSize,
                    TotalItems   = repository.RolesInfo.Count()
                }
            };

            return(View(viewModel));
        }
示例#3
0
        public void Can_Send_Pagination_View_Model()
        {
            //布置——创建模仿存储库
            Mock <IRoleInfoRepository> mock = new Mock <IRoleInfoRepository>();

            mock.Setup(m => m.RolesInfo).Returns(new RoleInfo[] {    //使用Setup更改RolesInfo的结果,不适用DI,而是用后续的Return返回结果
                new RoleInfo {
                    RoleID = new Guid(), RoleName = "R1", RoleEN = "REN1"
                },
                new RoleInfo {
                    RoleID = new Guid(), RoleName = "R2", RoleEN = "REN2"
                },
                new RoleInfo {
                    RoleID = new Guid(), RoleName = "R3", RoleEN = "REN3"
                },
                new RoleInfo {
                    RoleID = new Guid(), RoleName = "R4", RoleEN = "REN4"
                },
                new RoleInfo {
                    RoleID = new Guid(), RoleName = "R5", RoleEN = "REN5"
                },
                new RoleInfo {
                    RoleID = new Guid(), RoleName = "R6", RoleEN = "REN6"
                }
            }.AsQueryable());

            //布置——创建一个控制器,设置页面大小为3
            RolesController controller = new RolesController(mock.Object)
            {
                PageSize = 3
            };

            //动作
            RolesInfoViewModel result = (RolesInfoViewModel)controller.List(2).Model;

            //断言
            PagingInfo pageInfo = result.pagingInfo;

            Assert.AreEqual(pageInfo.CurrentPage, 2);
            Assert.AreEqual(pageInfo.ItemsPerPage, 3);
            Assert.AreEqual(pageInfo.TotalItems, 6);
            Assert.AreEqual(pageInfo.TotalPage, 2);
        }
示例#4
0
        public void Can_Paginate()
        {
            //布置
            //——创建模仿存储库
            Mock <IRoleInfoRepository> mock = new Mock <IRoleInfoRepository>();

            mock.Setup(m => m.RolesInfo).Returns(new RoleInfo[] {    //使用Setup更改RolesInfo的结果,不适用DI,而是用后续的Return返回结果
                new RoleInfo {
                    RoleID = new Guid(), RoleName = "R1", RoleEN = "REN1"
                },
                new RoleInfo {
                    RoleID = new Guid(), RoleName = "R2", RoleEN = "REN2"
                },
                new RoleInfo {
                    RoleID = new Guid(), RoleName = "R3", RoleEN = "REN3"
                },
                new RoleInfo {
                    RoleID = new Guid(), RoleName = "R4", RoleEN = "REN4"
                },
                new RoleInfo {
                    RoleID = new Guid(), RoleName = "R5", RoleEN = "REN5"
                },
                new RoleInfo {
                    RoleID = new Guid(), RoleName = "R6", RoleEN = "REN6"
                }
            }.AsQueryable());

            RolesController controller = new RolesController(mock.Object);

            controller.PageSize = 3;

            //动作
            RolesInfoViewModel result = (RolesInfoViewModel)controller.List(2).Model;

            //断言
            RoleInfo[] roleArray = result.RolesInfo.ToArray();
            Assert.IsTrue(roleArray.Length == 3);
            Assert.AreEqual(roleArray[0].RoleName, "R4");
            Assert.AreEqual(roleArray[1].RoleName, "R5");
        }