首页 > 代码库 > [工作流RoadFlow迁移至Core-3]Home-Index

[工作流RoadFlow迁移至Core-3]Home-Index

源代码解释:

1.依赖注入3个业务层接口:IUsersBll、IUsersRoleBll、IRoleBll,并初始化

2.CurrentUserId和CurrentUser 在父类MyController中实现(public class HomeController : MyController)

private readonly IUsersRoleBll _buserRole;
        private readonly IRoleBll _brole;

        public HomeController(IUsersBll busers, IUsersRoleBll usersRoleBll, IRoleBll brole) : base(busers)
        {
            _buserRole = usersRoleBll;
            _brole = brole;
        }

        //
        // GET: /Home/
        public ActionResult Index()
        {
            var roles = _buserRole.GetByUserId(CurrentUserId);
            ViewBag.CurrentUserID = CurrentUserId;
            ViewBag.RoleLength = roles.Count;
            ViewBag.DefaultRoleID = string.Empty;
            ViewBag.RolesOptions = string.Empty;
            if (roles.Count > 0)
            {
                var mainRole = roles.Find(p => p.IsDefault);
                ViewBag.defaultRoleID = mainRole != null ? mainRole.RoleId.ToString() : roles.First().RoleId.ToString();
                List<Role> roleList = new List<Role>();
                foreach (var role in roles)
                {
                    var role1 = _brole.Get(role.RoleId);
                    if (role1 != null)
                    {
                        roleList.Add(role1);
                    }
                }

                ViewBag.RolesOptions = _brole.GetRoleOptions("", "", roleList);
            }

            var user = CurrentUser;

            ViewBag.UserName = user != null ? user.Name : "";
            ViewBag.DateTime = DateTimeNew.Now.ToDateWeekString();

            return View();
        }

  测试代码

[Fact]
        public void HomeIndex正常显示()
        {
            // Arange
            // -1)
            Users newUsers = new Users()
            {
                Name = "朱杯"
            };

            // 0)
            var httpContext = new DefaultHttpContext();
            httpContext.Session = new TestSession();
            httpContext.Session.SetString(Keys.SessionKeys.UserId.ToString(), "cc5884c0-2d85-4cdd-932c-97dac6ec7282");
            httpContext.Session.SetString(Keys.SessionKeys.User.ToString(), newUsers.ToJsonString());

            // 1)
            var mockUsersBll = Substitute.For<IUsersBll>();
            var mockUsersRoleBll = Substitute.For<IUsersRoleBll>();
            var mockRoleBll = Substitute.For<IRoleBll>();

            // 2)
            List<UsersRole> usersRolesList = new List<UsersRole>()
            {
                new UsersRole
                {
                    MemberId = Guid.Parse("cc5884c0-2d85-4cdd-932c-97dac6ec7282"),
                    RoleId = Guid.Parse("cc5884c0-2d85-4cdd-932c-97dac6ec7283"),
                    IsDefault = true
                },
                new UsersRole()
                {
                    MemberId = Guid.Parse("cc5884c0-2d85-4cdd-932c-97dac6ec7282"),
                    RoleId = Guid.Parse("cc5884c0-2d85-4cdd-932c-97dac6ec7284"),
                    IsDefault = false
                }
            };
            Role role1 = new Role()
            {
                Id = Guid.Parse("cc5884c0-2d85-4cdd-932c-97dac6ec7283"),
                Name = "财务",
                Note = "财务",
                UseMember = "财务菜单"
            };
            Role role2 = new Role()
            {
                Id = Guid.Parse("cc5884c0-2d85-4cdd-932c-97dac6ec7284"),
                Name = "销售",
                Note = "销售",
                UseMember = "销售菜单"
            };
            List<Role> roleList = new List<Role>()
            {
                role1,
                role2
            };


            // 3)
            mockUsersRoleBll.GetByUserId(Guid.Parse("cc5884c0-2d85-4cdd-932c-97dac6ec7282")).Returns(usersRolesList);
            mockRoleBll.Get(Guid.Parse("cc5884c0-2d85-4cdd-932c-97dac6ec7283")).Returns(role1);
            mockRoleBll.Get(Guid.Parse("cc5884c0-2d85-4cdd-932c-97dac6ec7284")).Returns(role2);
            //mockUsersBll.Get(Guid.Parse("cc5884c0-2d85-4cdd-932c-97dac6ec7282")).ReturnsNull();

            // 4)
            mockRoleBll.GetRoleOptions("", "", Arg.Any<List<Role>>()).Returns("<option value=http://www.mamicode.com/"cc5884c0-2d85-4cdd-932c-97dac6ec7283\" selected=\"selected\">财务</option>" +
                                                                 "<option value=http://www.mamicode.com/"cc5884c0-2d85-4cdd-932c-97dac6ec7284\" >销售</option>");

            // Act
            var controller = new HomeController(mockUsersBll, mockUsersRoleBll, mockRoleBll);
            controller.ControllerContext.HttpContext = httpContext;
            var result = controller.Index();
            var viewResult = result.ShouldBeOfType<ViewResult>();
            Console.WriteLine("测试测试测试");
            Console.WriteLine("ViewBag.RoleLength:-------------- {0} --------------", controller.ViewBag.RoleLength);
            Console.WriteLine("ViewBag.RolesOptions:-------------- {0} --------------", controller.ViewBag.RolesOptions);

        }

  

[工作流RoadFlow迁移至Core-3]Home-Index