public FamiliesControllerTests() { var list = new List <Family> { new Family { Id = 1, Name = "test 1", BusinessId = 2 }, new Family { Id = 2, Name = "test 2", BusinessId = 1 } }.AsQueryable(); var mockContext = Substitute.For <TtContext>(); var familyRepository = Substitute.For <Repository <Family> >(mockContext); var businessRepository = Substitute.For <Repository <Business> >(mockContext); _service = Substitute.For <FamilyService>(familyRepository, businessRepository); _service.GetList().Returns(list); _service.GetItem(Arg.Any <int>()).Returns(new Family { Id = 1, Name = "test 1", BusinessId = 1 }); _service.Create(Arg.Any <Family>()); _service.Update(Arg.Any <int>(), Arg.Any <Family>()); _service.Delete(Arg.Any <int>()); var mockLogger = Substitute.For <ILoggerFactory>(); _controller = new FamiliesController(_service, mockLogger); }
public async void Update_ErrorsOccurred_ShouldReturnErrorResponse() { var mock = new ServiceMockFacade <IFamilyService, IFamilyRepository>(); var model = new ApiFamilyServerRequestModel(); var validatorMock = new Mock <IApiFamilyServerRequestModelValidator>(); validatorMock.Setup(x => x.ValidateUpdateAsync(It.IsAny <int>(), It.IsAny <ApiFamilyServerRequestModel>())).Returns(Task.FromResult(new ValidationResult(new List <ValidationFailure>() { new ValidationFailure("text", "test") }))); mock.RepositoryMock.Setup(x => x.Get(It.IsAny <int>())).Returns(Task.FromResult(new Family())); var service = new FamilyService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, validatorMock.Object, mock.DALMapperMockFactory.DALFamilyMapperMock, mock.DALMapperMockFactory.DALStudentMapperMock); UpdateResponse <ApiFamilyServerResponseModel> response = await service.Update(default(int), model); response.Should().NotBeNull(); response.Success.Should().BeFalse(); validatorMock.Verify(x => x.ValidateUpdateAsync(It.IsAny <int>(), It.IsAny <ApiFamilyServerRequestModel>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <FamilyUpdatedNotification>(), It.IsAny <CancellationToken>()), Times.Never()); }
public ActionResult Edit(int id, Family family) { try { familyService.Update(family); return(RedirectToAction("Index")); } catch { return(View()); } }
public async void Update() { var mock = new ServiceMockFacade <IFamilyRepository>(); var model = new ApiFamilyRequestModel(); mock.RepositoryMock.Setup(x => x.Create(It.IsAny <Family>())).Returns(Task.FromResult(new Family())); mock.RepositoryMock.Setup(x => x.Get(It.IsAny <int>())).Returns(Task.FromResult(new Family())); var service = new FamilyService(mock.LoggerMock.Object, mock.RepositoryMock.Object, mock.ModelValidatorMockFactory.FamilyModelValidatorMock.Object, mock.BOLMapperMockFactory.BOLFamilyMapperMock, mock.DALMapperMockFactory.DALFamilyMapperMock, mock.BOLMapperMockFactory.BOLStudentMapperMock, mock.DALMapperMockFactory.DALStudentMapperMock); UpdateResponse <ApiFamilyResponseModel> response = await service.Update(default(int), model); response.Should().NotBeNull(); mock.ModelValidatorMockFactory.FamilyModelValidatorMock.Verify(x => x.ValidateUpdateAsync(It.IsAny <int>(), It.IsAny <ApiFamilyRequestModel>())); mock.RepositoryMock.Verify(x => x.Update(It.IsAny <Family>())); }
public void FamilyService_Update_Calls_UnitOfWork_Commit_Method() { // Create test data var family = new Family { Id = TestConstants.ID_Exists, WifeId = TestConstants.ID_WifeId, HusbandId = TestConstants.ID_HusbandId }; //Create Mock var mockRepository = new Mock<IRepository<Family>>(); _mockUnitOfWork.Setup(d => d.GetRepository<Family>()).Returns(mockRepository.Object); //Arrange _service = new FamilyService(_mockUnitOfWork.Object); //Act _service.Update(family); //Assert _mockUnitOfWork.Verify(db => db.Commit()); }
public void FamilyService_Update_Calls_Repository_Update_Method_With_The_Same_Family_Object_It_Recieved() { // Create test data var family = new Family { Id = TestConstants.ID_Exists, WifeId = TestConstants.ID_WifeId, HusbandId = TestConstants.ID_HusbandId }; //Create Mock var mockRepository = new Mock<IRepository<Family>>(); _mockUnitOfWork.Setup(d => d.GetRepository<Family>()).Returns(mockRepository.Object); //Arrange _service = new FamilyService(_mockUnitOfWork.Object); //Act _service.Update(family); //Assert mockRepository.Verify(r => r.Update(family)); }
public void FamilyService_Update_Throws_On_Null_Family() { //Arrange _service = new FamilyService(_mockUnitOfWork.Object); //Assert Assert.Throws<ArgumentNullException>(() => _service.Update(null)); }
public ActionResult EditSave(Employee employee) { if (employee.Name == null) { return(Content("请输入姓名!")); } if (employee.nl == 0) { return(Content("请输入年龄!")); } if (employee.xb == null) { return(Content("请选择性别!")); } string strZp = Request.Form["strZp"].ToString(); string strSfzt = Request.Form["strSfzt"].ToString(); HttpFileCollectionBase files = Request.Files; HttpPostedFileBase FileSfzt = files["sfzt"]; string FileName = FileSfzt.FileName; //上传的原文件名 if (FileName != "") //换了图片 { string guidSfzt = ""; if (FileName != null && FileName != "") { string FileType = FileName.Substring(FileName.LastIndexOf(".") + 1); //得到文件的后缀名 guidSfzt = employee.Code + "-SFZ-" + System.Guid.NewGuid().ToString() + "." + FileType; //得到重命名的文件名 FileSfzt.SaveAs(Server.MapPath("/Upload/") + guidSfzt); //保存操作 } employee.sfzt = guidSfzt; } else { employee.sfzt = strSfzt; } HttpPostedFileBase FileZp = files["Zp"]; string FileNameZp = FileZp.FileName; //上传的原文件名 if (FileNameZp != "") //换了图片 { string guidZp = ""; if (FileNameZp != null && FileNameZp != "") { string FileType = FileNameZp.Substring(FileNameZp.LastIndexOf(".") + 1); //得到文件的后缀名 guidZp = employee.Code + "-ZP-" + System.Guid.NewGuid().ToString() + "." + FileType; //得到重命名的文件名 FileZp.SaveAs(Server.MapPath("/Upload/") + guidZp); //保存操作 } employee.zp = guidZp; } else { employee.zp = strZp; } string jtStr = Request.Form["jtStr"].ToString(); string gzStr = Request.Form["gzStr"].ToString(); string pxStr = Request.Form["pxStr"].ToString(); JavaScriptSerializer js = new JavaScriptSerializer(); //处理家庭成员 int jtCount = FamilyService.GetRecordCoutn(u => u.EmployeeID == employee.ID); List <Family> listJt = js.Deserialize <List <Family> >(jtStr); for (int j = 0; j < listJt.Count; j++) { Family family1 = listJt[j]; family1.Employee = employee; family1.EmployeeID = employee.ID; family1.ModifiedBy = LoginUserInfo == null ? 0 : LoginUserInfo.ID; family1.ModifiedTIme = DateTime.Now; if (family1.Code == null) { family1 = initFamily(family1); family1.Code = employee.Code + "-F" + (jtCount + 1).ToString(); FamilyService.Add(family1); } else { family1.ModifiedBy = LoginUserInfo == null ? 0 : LoginUserInfo.ID; family1.ModifiedTIme = DateTime.Now; FamilyService.Update(family1); } } FamilyService.SaveChanges(); //处理工作经历 int gzCount = ExperienceService.GetRecordCoutn(u => u.EmployeeID == employee.ID); List <Experience> listGz = js.Deserialize <List <Experience> >(gzStr); for (int i = 0; i < listGz.Count; i++) { Experience experience = listGz[i]; experience.Employee = employee; experience.EmployeeID = employee.ID; experience.ModifiedBy = LoginUserInfo == null ? 0 : LoginUserInfo.ID; experience.ModifiedTIme = DateTime.Now; if (experience.Code == null) { experience = initExperience(experience); experience.Code = employee.Code + "-E" + (gzCount + 1).ToString(); ExperienceService.Add(experience); } else { experience.ModifiedBy = LoginUserInfo == null ? 0 : LoginUserInfo.ID; experience.ModifiedTIme = DateTime.Now; ExperienceService.Update(experience); } } //处理培训经历 int pxCount = TrainService.GetRecordCoutn(u => u.EmployeeID == employee.ID); List <Train> listPx = js.Deserialize <List <Train> >(pxStr); for (int i = 0; i < listPx.Count; i++) { Train train = listPx[i]; train.Employee = employee; train.EmployeeID = employee.ID; train.ModifiedBy = LoginUserInfo == null ? 0 : LoginUserInfo.ID; train.ModifiedTIme = DateTime.Now; if (train.Code == null) { train = initTrain(train); train.Code = employee.Code + "-T" + (pxCount + 1).ToString(); TrainService.Add(train); } else { train.ModifiedBy = LoginUserInfo == null ? 0 : LoginUserInfo.ID; train.ModifiedTIme = DateTime.Now; TrainService.Update(train); } } employee.ModifiedBy = LoginUserInfo == null ? 0 : LoginUserInfo.ID; employee.ModifiedTIme = DateTime.Now; if (EmployeeService.Update(employee)) { TrainService.SaveChanges(); ExperienceService.SaveChanges(); FamilyService.SaveChanges(); EmployeeService.SaveChanges(); return(Content("ok")); } return(Content("修改失败了!")); }