public void Can_Edit_Role() { //布置 //——创建模仿存储库 RoleInfo[] roles = new RoleInfo[]{ new RoleInfo{RoleID = Guid.NewGuid(), RoleName = "R1",RoleEN="REN1"}, new RoleInfo{RoleID = Guid.NewGuid(), RoleName = "R2",RoleEN="REN2"}, new RoleInfo{RoleID = Guid.NewGuid(), RoleName = "R3",RoleEN="REN3"}, new RoleInfo{RoleID = Guid.NewGuid(), RoleName = "R4",RoleEN="REN4"}, new RoleInfo{RoleID = Guid.NewGuid(), RoleName = "R5",RoleEN="REN5"}, new RoleInfo{RoleID = Guid.NewGuid(), RoleName = "R6",RoleEN="REN6"} }; Mock<IRoleInfoRepository> mock = new Mock<IRoleInfoRepository>(); mock.Setup(m => m.RolesInfo).Returns(roles.AsQueryable()); //使用Setup更改RolesInfo的结果,不适用DI,而是用后续的Return返回结果 //布置——创建控制器 RolesController controller = new RolesController(mock.Object); //动作 RoleInfo r1 = controller.Edit(roles[1].RoleID).Model as RoleInfo; RoleInfo r2 = controller.Edit(roles[2].RoleID).Model as RoleInfo; RoleInfo r3 = controller.Edit(roles[3].RoleID).Model as RoleInfo; //断言 Assert.AreEqual(r1.RoleName, "R2"); Assert.AreEqual(r2.RoleName, "R3"); Assert.AreEqual(r3.RoleName, "R4"); }
public void Cannot_Edit_Nonexistent_Product() { //布置 //——创建模仿存储库 RoleInfo[] roles = new RoleInfo[]{ new RoleInfo{RoleID = Guid.NewGuid(), RoleName = "R1",RoleEN="REN1"}, new RoleInfo{RoleID = Guid.NewGuid(), RoleName = "R2",RoleEN="REN2"}, new RoleInfo{RoleID = Guid.NewGuid(), RoleName = "R3",RoleEN="REN3"} }; Mock<IRoleInfoRepository> mock = new Mock<IRoleInfoRepository>(); mock.Setup(m => m.RolesInfo).Returns(roles.AsQueryable()); //布置——创建控制器 RolesController controller = new RolesController(mock.Object); //动作 RoleInfo r1 = controller.Edit(Guid.NewGuid()).Model as RoleInfo; //断言 Assert.IsNull(r1); }
public void Can_Delete_Valid_Roles() { //布置——创建一个角色 RoleInfo role = new RoleInfo { RoleID = Guid.NewGuid(), RoleName = "R1" }; //布置——创建模仿存储库 Mock<IRoleInfoRepository> mock = new Mock<IRoleInfoRepository>(); RoleInfo[] roles = new RoleInfo[]{ new RoleInfo{RoleID = Guid.NewGuid(), RoleName = "R1",RoleEN="REN1"}, role, new RoleInfo{RoleID = Guid.NewGuid(), RoleName = "R3",RoleEN="REN3"} }; mock.Setup(m => m.RolesInfo).Returns(roles.AsQueryable()); //布置——创建控制器 RolesController controller = new RolesController(mock.Object); //动作——删除产品 controller.Delete(role.RoleID); //断言 mock.Verify(m => m.DeleteRoleInfo(role)); }