public async Task Create_POST_calls_AddCategoryAsync_and_redirects_to_index() { // Arrange var category = new CategoryCrudVm(); // Act var result = await _controller.Create(category); var redirectResult = result as RedirectToActionResult; // Assert _mockService.Verify(m => m.AddCategoryAsync(category.Name, category.Amount, category.Type), Times.Once()); Assert.IsNotNull(redirectResult); Assert.AreEqual("Index", redirectResult.ActionName); }
public async Task Edit_POST_throws_Exceptions_not_of_type_ExpesneTrackerException() { // Arrange var category = new CategoryCrudVm { NavId = 1 }; _mockService.Setup(m => m.UpdateCategoryAsync(It.IsAny <int>(), It.IsAny <double>(), It.IsAny <DateTime>(), It.IsAny <BudgetType>())).ThrowsAsync(new Exception()); SetupControllerRouteData(_controller, "id", 1); // Act & Assert await Assert.ThrowsExceptionAsync <Exception>(() => _controller.Edit(category)); }
public async Task Edit_POST_throws_ConcurrencyExceptions_if_the_category_exists() { // Arrange var category = new CategoryCrudVm { NavId = 1 }; _mockService.Setup(m => m.UpdateCategoryAsync(It.IsAny <int>(), It.IsAny <double>(), It.IsAny <DateTime>(), It.IsAny <BudgetType>())).ThrowsAsync(new ConcurrencyException()); _mockService.Setup(m => m.CategoryExists(It.IsAny <int>())).Returns(true); SetupControllerRouteData(_controller, "id", 1); // Act & Assert await Assert.ThrowsExceptionAsync <ConcurrencyException>(() => _controller.Edit(category)); }
public async Task Edit_POST_returns_NotFound_when_ExpenseTrackerException_thrown() { // Arrange var category = new CategoryCrudVm { NavId = 1 }; _mockService.Setup(m => m.UpdateCategoryAsync(It.IsAny <int>(), It.IsAny <double>(), It.IsAny <DateTime>(), It.IsAny <BudgetType>())).ThrowsAsync(new ExpenseTrackerException()); SetupControllerRouteData(_controller, "id", 1); // Act var result = await _controller.Edit(category); // Assert Assert.IsInstanceOfType(result, typeof(NotFoundResult)); }
public async Task Edit_POST_returns_to_view_when_ModelValidationException_thrown() { // Arrange var category = new CategoryCrudVm { NavId = 1 }; SetupControllerRouteData(_controller, "id", category.NavId); _mockService.Setup(m => m.UpdateCategoryAsync(It.IsAny <int>(), It.IsAny <double>(), It.IsAny <DateTime>(), It.IsAny <BudgetType>())) .ThrowsAsync(new ModelValidationException()); // Act var result = await _controller.Edit(category); var viewResult = result as ViewResult; var model = viewResult.Model as CategoryCrudVm; // Assert Assert.IsNotNull(viewResult); Assert.AreEqual("Edit", viewResult.ViewName); Assert.AreSame(category, model); }
public async Task Edit_POST_calls_UpdateCategoryAsync_and_redirects_to_Index() { // Arrange var category = new CategoryCrudVm { NavId = 1, Amount = 20.12, EffectiveFrom = DateTime.Parse("1/1/2018"), Type = BudgetType.Expense }; SetupControllerRouteData(_controller, "id", 1); // Act var result = await _controller.Edit(category); var redirectResult = result as RedirectToActionResult; // Assert _mockService.Verify(m => m.UpdateCategoryAsync(category.NavId, category.Amount, category.EffectiveFrom, category.Type), Times.Once()); Assert.IsNotNull(redirectResult); Assert.AreEqual("Index", redirectResult.ActionName); }
public async Task Create_POST_adds_modelstate_error_when_UniqueConstraintViolationException_thrown() { // Arrange var category = new CategoryCrudVm { Name = "test" }; _mockService.Setup(m => m.AddCategoryAsync(It.IsAny <string>(), It.IsAny <double>(), It.IsAny <BudgetType>())) .ThrowsAsync(new ModelValidationException()); // Act var result = await _controller.Create(category); var viewResult = result as ViewResult; var model = viewResult.Model as CategoryCrudVm; // Assert Assert.AreEqual(1, _controller.ModelState.ErrorCount); Assert.IsNotNull(viewResult); Assert.AreEqual("Create", viewResult.ViewName); Assert.AreSame(category, model); }
public async Task Edit_POST_returns_to_Edit_and_sets_ModelState_Error_when_InvalidDateException_thrown() { // Arrange var category = new CategoryCrudVm { NavId = 1 }; _mockService.Setup(m => m.UpdateCategoryAsync(It.IsAny <int>(), It.IsAny <double>(), It.IsAny <DateTime>(), It.IsAny <BudgetType>())).ThrowsAsync(new InvalidDateExpection("Test Message")); SetupControllerRouteData(_controller, "id", 1); // Act var result = await _controller.Edit(category); var viewResult = result as ViewResult; // Assert Assert.IsNotNull(viewResult); Assert.AreEqual("Edit", viewResult.ViewName); Assert.AreEqual(1, _controller.ViewData.ModelState.ErrorCount); }