public void Get_gets_course_types_and_returns_edit_view_with_course_model() { #region Arrange using (Mock.Record()) { Expect.Call(CourseService.GetById(1)).Return(Course); Expect.Call(CourseService.GetAllCourseTypes()).Return(CourseTypesList); } #endregion #region Act ViewResult view; using (Mock.Playback()) { view = (ViewResult)CourseController.Edit(1); } #endregion #region Assert Assert.IsEmpty(view.ViewName); Assert.IsNotNull(view.ViewBag.CourseTypes); Assert.That(view.ViewBag.CourseTypes, Is.InstanceOf(typeof(SelectList))); Assert.IsNotNull(view.ViewData.Model); Assert.That(view.ViewData.Model, Is.InstanceOf(typeof(CourseDto))); #endregion }
public async void CanUpdateCourse() { DbContextOptions <SchoolDbContext> options = new DbContextOptionsBuilder <SchoolDbContext>() .UseInMemoryDatabase("CourseUpdateTestDb") .Options; using (SchoolDbContext context = new SchoolDbContext(options)) { Course course1 = new Course(); course1.ID = 42; course1.Name = "Advanced Anger Management"; course1.Teacher = "Bob Saget"; course1.CourseTerm = CourseTerm.Fall2018; CourseController testCC = new CourseController(context); var x = await testCC.Create(course1); course1.Name = "Making Unit Tests"; course1.Teacher = "Ron Testmaster"; course1.CourseTerm = CourseTerm.Winter2018; var update1 = await testCC.Edit(42, course1); var result1 = context.Courses.Where(a => a.Name == "Making Unit Tests"); var result2 = context.Courses.Where(a => a.Name == "Advanced Anger Management"); //Assert Assert.IsAssignableFrom <IActionResult>(x); Assert.Equal(1, result1.Count()); Assert.Equal(0, result2.Count()); } }
[Test] public void Post_if_model_state_false_then_return_edit_view() { #region Arrange //Faking ModelState.IsValid = false CourseController.ModelState.Add("testError", new ModelState()); CourseController.ModelState.AddModelError("testError", "test"); using (Mock.Record()) { Expect.Call(CourseService.Update(ErrorCourse, false)).Repeat.Never(); } #endregion #region Act ViewResult view; using (Mock.Playback()) { view = (ViewResult)CourseController.Edit(Course); } #endregion #region Assert Assert.IsEmpty(view.ViewName); Assert.That(view.ViewBag.Error, Is.EqualTo(elearn.Common.ErrorMessages.Course.ModelUpdateError)); #endregion }
public void Edit_ReturnsBadRequest_WhenCourseParameterIsNull() { //Arrange Course course = null; CourseService courseService = Substitute.For <CourseService>(); CourseController controller = new CourseController(courseService, null, null); //Act IActionResult actual = controller.Edit(course); //Assert actual.Should().BeAssignableTo <BadRequestResult>(); }
public void Edit_ReturnNotFound_WhenCourseDoesNotExist() { //Arrange CourseService courseService = Substitute.For <CourseService>(); CourseController controller = new CourseController(courseService, null, null); //Act IActionResult actual = controller.Edit(0); //Assert var expected = new NotFoundResult(); actual.Should().BeEquivalentTo(expected); }
public void Edit_Get_ReturnsNotFound_WhenNonExistingCourseId() { // Arrange int nonExistingCourseId = 0; var courseServiceMock = Substitute.For <CourseService>(); courseServiceMock.GetCourse(nonExistingCourseId).Returns(default(Course)); var controller = new CourseController(courseServiceMock, null, null, null, null); // Act var result = controller.Edit(nonExistingCourseId); // Assert var viewResult = Assert.IsType <NotFoundResult>(result); }
public void Edit_Get_ReturnsViewResult_WithCource() { // Arrange int existingCourseId = 42; var courseServiceMock = Substitute.For <CourseService>(); courseServiceMock.GetCourse(existingCourseId).Returns(new Course()); var controller = new CourseController(courseServiceMock, null, null, null, null); // Act var result = controller.Edit(existingCourseId); // Assert var viewResult = Assert.IsType <ViewResult>(result); var model = Assert.IsType <Course>(viewResult.Model); }
public void Edit_Post_RedirectToCourses() { // Arrange Course courseForEditing = new Course(); var courseSericeMock = Substitute.For <CourseService>(); courseSericeMock.CreateCourse(courseForEditing).Returns(true); var controller = new CourseController(courseSericeMock, null, null, null, null); // Act var result = controller.Edit(courseForEditing); // Assert var redirectToActionResult = Assert.IsType <RedirectToActionResult>(result); Assert.Null(redirectToActionResult.ControllerName); Assert.Equal("Courses", redirectToActionResult.ActionName); }
public void Edit_RedirectsToCourses_WhenCourseParameterIsNotNull() { //Arrange Course course = new Course() { Name = "Test course" }; CourseService courseService = Substitute.For <CourseService>(); courseService.GetCourse(5).Returns(course); CourseController controller = new CourseController(courseService, null, null); //Act var actionResult = controller.Edit(course); courseService.Received().UpdateCourse(course); // Assert actionResult.Should().BeOfType <RedirectToActionResult>().Which.ActionName.Should().Be("Courses"); }
public void Edit_ReturnViewResultWithModel_WhenCourseExists() { //Arrange Course course = new Course { Name = "Test one" }; CourseService courseService = Substitute.For <CourseService>(); courseService.GetCourse(5).Returns(course); CourseController controller = new CourseController(courseService, null, null); //Act IActionResult actual = controller.Edit(5); //Assert actual.Should().BeAssignableTo <ViewResult>(); ViewResult viewResult = (ViewResult)actual; viewResult.Model.Should().BeEquivalentTo(course); }
public void Post_updates_course_then_redirects_to_the_details_action() { #region Arrange using (Mock.Record()) { Expect.Call(CourseService.Update(Course, false)).Return(true); } #endregion #region Act RedirectToRouteResult redirect; using (Mock.Playback()) { redirect = (RedirectToRouteResult)CourseController.Edit(Course); } #endregion #region Assert redirect.AssertActionRedirect().ToAction("Details").WithParameter("id", 1); #endregion }
public void Post_if_update_to_db_fails_then_return_error_view() { #region Arrange using (Mock.Record()) { Expect.Call(CourseService.Update(Course, false)).Return(false); } #endregion #region Act ViewResult view; using (Mock.Playback()) { view = (ViewResult)CourseController.Edit(Course); } #endregion #region Assert Assert.That(view.ViewName, Is.EqualTo("Error")); Assert.That(view.ViewBag.Error, Is.EqualTo(elearn.Common.ErrorMessages.Course.UpdateToDbError)); #endregion }
public void CannotEditWithMissingValues() { Mock <IGenericService <Course> > courseServiceMock = new Mock <IGenericService <Course> >(); Mock <IActivityLoggerService> loggerMock = new Mock <IActivityLoggerService>(); Mock <IObjectFinderService <Module> > moduleFinderMock = new Mock <IObjectFinderService <Module> >(); Mock <IGenericService <Module> > moduleServiceMock = new Mock <IGenericService <Module> >(); Mock <IManyToManyMapperService <Course, CourseModule, Module> > mapperServiceMock = new Mock <IManyToManyMapperService <Course, CourseModule, Module> >(); Mock <IGenericService <ExamProgram> > examProgramServiceMock = new Mock <IGenericService <ExamProgram> >(); Mock <IGenericService <Teacher> > teacherServiceMock = new Mock <IGenericService <Teacher> >(); CourseCreateUpdateViewModel course = new CourseCreateUpdateViewModel() { Id = 100, Name = null, Description = "Alle opdrachten in deze periode zijn gekoppeld aan vakken; dit kunnen individuele of groepsopdrachten zijn; er zijn geen opdrachten die meerdere vakken betreffen. Daarom is een verdere omschrijving hier niet nodig." }; ClaimsPrincipal identity = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.NameIdentifier, "123") })); teacherServiceMock.Setup(m => m.FindById(It.IsAny <int>(), It.IsAny <string[]>())).Returns(new Teacher()); moduleFinderMock.Setup(m => m.AreIdsValid(It.IsAny <string[]>())).Returns(true); moduleFinderMock.Setup(m => m.GetObjects(It.IsAny <int[]>())).Returns((int[] ids) => { return(new List <Module>()); }); mapperServiceMock.Setup(m => m.GetMappedEntities(It.IsAny <Course>(), It.IsAny <ICollection <Module> >())).Returns <Course, ICollection <Module> >((model, modules) => { return(new List <CourseModule>()); }); courseServiceMock.Setup(m => m.Update(It.IsAny <Course>())).Returns((Course model) => { if (!string.IsNullOrWhiteSpace(model.Name)) { return(1); } return(0); }); loggerMock.Setup(m => m.Create(It.IsAny <string>())); CourseController controller = new CourseController(courseServiceMock.Object, moduleServiceMock.Object, moduleFinderMock.Object, mapperServiceMock.Object, examProgramServiceMock.Object, teacherServiceMock.Object) { ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { User = identity } } }; ViewResult result = controller.Edit(course) as ViewResult; Assert.NotNull(result); Assert.NotNull(result.Model); Assert.True(string.IsNullOrEmpty(result.ViewName) || result.ViewName == "Edit"); }