public void Delete_ThrowsArgumentException_WhenProductsAreRelated() { // Arrange var categories = new List <Category> { new Category { Name = "TestCategory1", Id = 1 }, new Category { Name = "TestCategory2", Id = 2 } }.AsQueryable(); var products = new List <Product> { new Product { Name = "TestProduct1", CategoryId = 1 } }.AsQueryable(); var categorymockDbSet = new Mock <DbSet <Category> >(); categorymockDbSet.As <IQueryable <Category> >().Setup(m => m.Provider).Returns(categories.Provider); categorymockDbSet.As <IQueryable <Category> >().Setup(m => m.Expression).Returns(categories.Expression); categorymockDbSet.As <IQueryable <Category> >().Setup(m => m.ElementType).Returns(categories.ElementType); categorymockDbSet.As <IQueryable <Category> >().Setup(m => m.GetEnumerator()).Returns(categories.GetEnumerator()); var productmockDbSet = new Mock <DbSet <Product> >(); productmockDbSet.As <IQueryable <Product> >().Setup(m => m.Provider).Returns(products.Provider); productmockDbSet.As <IQueryable <Product> >().Setup(m => m.Expression).Returns(products.Expression); productmockDbSet.As <IQueryable <Product> >().Setup(m => m.ElementType).Returns(products.ElementType); productmockDbSet.As <IQueryable <Product> >().Setup(m => m.GetEnumerator()).Returns(products.GetEnumerator()); var mockContext = new Mock <EazyCartContext>(); mockContext.Setup(c => c.Categories).Returns(categorymockDbSet.Object); mockContext.Setup(c => c.Products).Returns(productmockDbSet.Object); var categoryBusiness = new CategoryBusiness(mockContext.Object); // Act & Assert try { int idToDelete = 1; categoryBusiness.Delete(idToDelete); Assert.Fail("No exception was thrown."); } catch (ArgumentException ex) { string expectedMessage = string.Format("One or more products are related to this category."); Assert.AreEqual(expectedMessage, ex.Message, "Wrong exception was thrown."); } }
public IActionResult Delete(int id) { var model = _categoryBusiness.Delete(id); if (!model) { return(NotFound()); } return(Ok()); }
public void Delete_SuccessfullyDeletesCategory_WhenNoProductsAreRelated() { // Arrange var categories = new List <Category> { new Category { Name = "TestCategory1", Id = 1 }, new Category { Name = "TestCategory2", Id = 2 } }.AsQueryable(); var products = new List <Product> { new Product { Name = "TestProduct1", CategoryId = 1 } }.AsQueryable(); var categorymockDbSet = new Mock <DbSet <Category> >(); categorymockDbSet.As <IQueryable <Category> >().Setup(m => m.Provider).Returns(categories.Provider); categorymockDbSet.As <IQueryable <Category> >().Setup(m => m.Expression).Returns(categories.Expression); categorymockDbSet.As <IQueryable <Category> >().Setup(m => m.ElementType).Returns(categories.ElementType); categorymockDbSet.As <IQueryable <Category> >().Setup(m => m.GetEnumerator()).Returns(categories.GetEnumerator()); var productmockDbSet = new Mock <DbSet <Product> >(); productmockDbSet.As <IQueryable <Product> >().Setup(m => m.Provider).Returns(products.Provider); productmockDbSet.As <IQueryable <Product> >().Setup(m => m.Expression).Returns(products.Expression); productmockDbSet.As <IQueryable <Product> >().Setup(m => m.ElementType).Returns(products.ElementType); productmockDbSet.As <IQueryable <Product> >().Setup(m => m.GetEnumerator()).Returns(products.GetEnumerator()); var mockContext = new Mock <EazyCartContext>(); mockContext.Setup(c => c.Categories).Returns(categorymockDbSet.Object); mockContext.Setup(c => c.Products).Returns(productmockDbSet.Object); var categoryBusiness = new CategoryBusiness(mockContext.Object); // Act int idToDelete = 2; categoryBusiness.Delete(idToDelete); // Assert categorymockDbSet.Verify(m => m.Remove(It.IsAny <Category>()), Times.Once()); mockContext.Verify(m => m.SaveChanges(), Times.Once()); }
public ActionResult Delete(Category model) { try { // TODO: Add delete logic here //var lista = CategoryBusiness.GetCategoryList(); //Student category = lista.Find(x => x.Id == id); //lista.Remove(category); CategoryBusiness.Delete(model); return(RedirectToAction("Index")); } catch { return(View()); } }
public JsonResult Delete(string stkns) { string JsonStr = ""; bool isSuccess = true; string message = "Delete Successful!"; _unitOfWork.BeginTransaction(); try { foreach (string tokenkey in stkns.Substring(1).Split('~')) { List <Category> categoryList = _categoryBusiness.GetListWT(x => x.TokenKey == tokenkey).ToList(); foreach (Category cat in categoryList) { int deleteId = Convert.ToInt32(cat.CategoryId); var br = _categoryBusiness.Find(deleteId); _categoryBusiness.Delete(br); _unitOfWork.SaveChanges(); } } _unitOfWork.Commit(); } catch (Exception ex) { _unitOfWork.Rollback(); message = "Delete Unsuccessful!"; throw ex; } finally { _unitOfWork.Dispose(); } TempData["Success"] = message; TempData["isSuccess"] = isSuccess.ToString(); JsonStr = "{\"message\":\"" + message + "\",\"isSuccess\":\"" + isSuccess + "\"}"; return(Json(JsonStr, JsonRequestBehavior.AllowGet)); }
public void Delete(int id) { business.Delete(id); }