public void IndexReturnsAViewResultWithAListOfBrainstormSessions() { var mockRepo = new Mock<IBrainstormSessionRepository>(); mockRepo.Setup(r => r.List()).Returns(GetTestSessions()); var controller = new HomeController(mockRepo.Object); var result = Assert.IsType<ViewResult>(controller.Index()); var model = Assert.IsAssignableFrom<IEnumerable<StormSessionViewModel>> (result.ViewData.Model); Assert.Equal(2, model.Count()); }
public void IndexPostReturnsAViewResultWhenModelStateIsInvalid() { var mockRepo = new Mock<IBrainstormSessionRepository>(); mockRepo.Setup(r => r.List()).Returns(GetTestSessions()); var controller = new HomeController(mockRepo.Object); controller.ModelState.AddModelError("SessionName", "Required"); var newSession = new HomeController.NewSessionModel(); var result = Assert.IsType<ViewResult>(controller.Index(newSession)); Assert.IsAssignableFrom<IEnumerable<StormSessionViewModel>> (result.ViewData.Model); }
public void IndexPostReturnsARedirectAndAddsSessionWhenModelStateIsValid() { var mockRepo = new Mock<IBrainstormSessionRepository>(); mockRepo.Setup(r => r.Add(It.IsAny<BrainstormSession>())).Verifiable(); var controller = new HomeController(mockRepo.Object); var newSession = new HomeController.NewSessionModel() { SessionName = "Test Name" }; var result = Assert.IsType<RedirectToActionResult> (controller.Index(newSession)); Assert.Equal("Home", result.ControllerName); Assert.Equal("Index", result.ActionName); mockRepo.Verify(); }
public async Task IndexPost_ReturnsBadRequestResult_WhenModelStateIsInvalid() { // Arrange var mockRepo = new Mock<IBrainstormSessionRepository>(); mockRepo.Setup(repo => repo.ListAsync()).Returns(Task.FromResult(GetTestSessions())); var controller = new HomeController(mockRepo.Object); controller.ModelState.AddModelError("SessionName", "Required"); var newSession = new HomeController.NewSessionModel(); // Act var result = await controller.Index(newSession); // Assert var badRequestResult = Assert.IsType<BadRequestObjectResult>(result); Assert.IsType<SerializableError>(badRequestResult.Value); }
public async Task Index_ReturnsAViewResult_WithAListOfBrainstormSessions() { // Arrange var mockRepo = new Mock<IBrainstormSessionRepository>(); mockRepo.Setup(repo => repo.ListAsync()).Returns(Task.FromResult(GetTestSessions())); var controller = new HomeController(mockRepo.Object); // Act var result = await controller.Index(); // Assert var viewResult = Assert.IsType<ViewResult>(result); var model = Assert.IsAssignableFrom<IEnumerable<StormSessionViewModel>>( viewResult.ViewData.Model); Assert.Equal(2, model.Count()); }
public async Task IndexPost_ReturnsARedirectAndAddsSession_WhenModelStateIsValid() { // Arrange var mockRepo = new Mock<IBrainstormSessionRepository>(); mockRepo.Setup(repo => repo.AddAsync(It.IsAny<BrainstormSession>())) .Returns(Task.CompletedTask) .Verifiable(); var controller = new HomeController(mockRepo.Object); var newSession = new HomeController.NewSessionModel() { SessionName = "Test Name" }; // Act var result = await controller.Index(newSession); // Assert var redirectToActionResult = Assert.IsType<RedirectToActionResult>(result); Assert.Null(redirectToActionResult.ControllerName); Assert.Equal("Index", redirectToActionResult.ActionName); mockRepo.Verify(); }