public void Can_Not_Save_Category_That_Already_Exists() { var category = new Category { Name = "Hockey" }; var result = controller.Edit(category); mock.Verify(m => m.SaveCategory(It.IsAny<Category>()), Times.Never()); result.AssertViewRendered().ForView(string.Empty); }
public ActionResult Edit(Category category) { if (ModelStateAndCategoryNameIsValid(category)) { if (CategoryAlreadyExists(category)) { this.Error("A category with that name already exists."); //ModelState.AddModelError("", "A category with that name already exists."); return View(category); } else { repository.SaveCategory(category); this.Success("The category has been saved"); return RedirectToAction("Index"); } } return View(category); }
/// <summary> /// Create a new Category object. /// </summary> /// <param name="categoryId">Initial value of the CategoryId property.</param> /// <param name="isActive">Initial value of the IsActive property.</param> public static Category CreateCategory(global::System.Int32 categoryId, global::System.Boolean isActive) { Category category = new Category(); category.CategoryId = categoryId; category.IsActive = isActive; return category; }
/// <summary> /// Deprecated Method for adding a new object to the Categories EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToCategories(Category category) { base.AddObject("Categories", category); }
private bool ModelStateAndCategoryNameIsValid(Category category) { return ModelState.IsValid && !string.IsNullOrEmpty(category.Name); }
private bool CategoryAlreadyExists(Category category) { Category existingCategory = repository.Categories .FirstOrDefault(c => c.Name == category.Name); return existingCategory != null && category.CategoryId == 0; }
public void SaveCategory(Category category) { if (category.CategoryId == 0) { context.AddToCategories(category); } else { try { context.UpdateDetachedEntity<Category>(category, x => x.CategoryId); } catch (Exception) { context.ApplyCurrentValues<Category>(category.EntityKey.EntitySetName, category); } } context.SaveChanges(); }
public void Can_Not_Save_Category_With_Empty_Name() { var category = new Category { Name = null }; var result = controller.Edit(category); mock.Verify(m => m.SaveCategory(It.IsAny<Category>()), Times.Never()); result.AssertViewRendered().ForView(string.Empty); }