public void ShouldEditCorrectly() { Mock <IGenericService <Exam> > examServiceMock = new Mock <IGenericService <Exam> >(); Exam exam = new Exam() { Id = 100, AttemptOne = DateTime.Now.Date, AttemptTwo = DateTime.Now.Date.AddDays(14), EC = 3, Duration = TimeSpan.FromMinutes(30), Compensatable = true, ExamType = "Vh:essaytoets + vh+att:gedragsassessment + vh:portfolio-assessment + vh:vaardigheidstoets(R)", Language = "NL", ResponsibleTeacher = new Teacher() { FirstName = "Robin", LastName = "Schellius" }, Module = new Module() { Name = "Client-side web frameworks", } }; ClaimsPrincipal identity = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.NameIdentifier, "123") })); examServiceMock.Setup(m => m.Update(It.IsAny <Exam>())).Returns((Exam model) => { if (!string.IsNullOrWhiteSpace(model.Name) && !string.IsNullOrWhiteSpace(model.Language) && !string.IsNullOrWhiteSpace(model.ResponsibleTeacher?.FullName) && !string.IsNullOrWhiteSpace(model.ExamType) && !string.IsNullOrWhiteSpace(model.Module?.Name)) { return(1); } return(0); }); ExamController controller = new ExamController(examServiceMock.Object) { ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { User = identity } } }; RedirectToActionResult result = controller.Edit(exam) as RedirectToActionResult; Assert.Equal("Index", result?.ActionName); }
public void CannotEditWithMissingValues() { Mock <IGenericService <Exam> > examServiceMock = new Mock <IGenericService <Exam> >(); Exam exam = new Exam() { Id = 100, AttemptOne = DateTime.Now.Date, AttemptTwo = DateTime.Now.Date.AddDays(14), EC = 3, Duration = TimeSpan.FromMinutes(30), Compensatable = true, ExamType = "Vh:essaytoets + vh+att:gedragsassessment + vh:portfolio-assessment + vh:vaardigheidstoets(R)", Language = "NL", ResponsibleTeacher = null, Module = new Module() { Name = "Client-side web frameworks", } }; ClaimsPrincipal identity = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.NameIdentifier, "123") })); examServiceMock.Setup(m => m.Update(It.IsAny <Exam>())).Returns((Exam model) => { if (!string.IsNullOrWhiteSpace(model.Name) && !string.IsNullOrWhiteSpace(model.Language) && !string.IsNullOrWhiteSpace(model.ResponsibleTeacher?.FullName) && !string.IsNullOrWhiteSpace(model.ExamType) && !string.IsNullOrWhiteSpace(model.Module?.Name)) { return(1); } return(0); }); ExamController controller = new ExamController(examServiceMock.Object) { ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { User = identity } } }; ViewResult result = controller.Edit(exam) as ViewResult; Assert.NotNull(result); Assert.NotNull(result.Model); Assert.True(string.IsNullOrEmpty(result.ViewName) || result.ViewName == "Edit"); }