public async void TestCreatePostMethod_SaveThroughRepository_WhenModelStateIsValid() { // Arrange var mockRepo = new Mock <IDanceRepository>(); mockRepo.Setup(repo => repo.SaveDanceCourseType(It.IsAny <DanceCourseType>())) .Returns(Task.CompletedTask) .Verifiable(); var controller = new DanceCourseTypesController(mockRepo.Object); DanceCourseTypeVM s = new DanceCourseTypeVM() { Title = "Hiphop", Description = "A description" }; // Act var result = await controller.Create(s); // Assert var redirectToActionResult = Assert.IsType <RedirectToActionResult>(result); Assert.Null(redirectToActionResult.ControllerName); Assert.Equal("Index", redirectToActionResult.ActionName); mockRepo.Verify(); }
public async void TestCreatePostMethod_ReturnsViewWithObjects_WhenModelStateIsInvalid() { // Arrange var mockRepo = new Mock <IDanceRepository>(); var controller = new DanceCourseTypesController(mockRepo.Object); controller.ModelState.AddModelError("Title", "Required"); var courseTypeVM = new DanceCourseTypeVM() { Title = "", Description = "CourseType Description" }; // Act var result = await controller.Create(courseTypeVM); // Assert var viewResult = Assert.IsType <ViewResult>(result); var model = Assert.IsAssignableFrom <DanceCourseTypeVM>(viewResult.ViewData.Model); Assert.IsType <DanceCourseTypeVM>(model); }