public void Add_SuccessfullyAddsCategory_WhenIdIsNotDuplicate() { // Arrange var categories = new List <Category>().AsQueryable(); var mockSet = new Mock <DbSet <Category> >(); mockSet.As <IQueryable <Category> >().Setup(m => m.Provider).Returns(categories.Provider); mockSet.As <IQueryable <Category> >().Setup(m => m.Expression).Returns(categories.Expression); mockSet.As <IQueryable <Category> >().Setup(m => m.ElementType).Returns(categories.ElementType); mockSet.As <IQueryable <Category> >().Setup(m => m.GetEnumerator()).Returns(categories.GetEnumerator()); var mockContext = new Mock <EazyCartContext>(); mockContext.Setup(m => m.Categories).Returns(mockSet.Object); var categoryBusiness = new CategoryBusiness(mockContext.Object); // Act categoryBusiness.Add("TestCategory1", 1); // Assert mockSet.Verify(m => m.Add(It.IsAny <Category>()), Times.Once()); mockContext.Verify(m => m.SaveChanges(), Times.Once()); }
private void buttonAddCategory_Click(object sender, EventArgs e) { Category newCategory = new Category(); newCategory.Name = textBoxCategoryName.Text; newCategory.Description = textBoxCategoryDescription.Text; categoryBusiness.Add(newCategory); getCategories(); }
public IActionResult Post([FromBody] Category viewModel) { if (!ModelState.IsValid) { return(BadRequest()); } var id = _categoryBusiness.Add(viewModel); return(CreatedAtAction(nameof(Get), new { Id = id }, viewModel)); }
public ActionResult Create(Category model) { try { // TODO: Add insert logic here CategoryBusiness.Add(model); return(RedirectToAction("Index")); } catch { return(View()); } }
private void buttonAddCat_Click(object sender, EventArgs e) { Category category = new Category(); category.Name = textBoxCatName.Text; category.Info = textBoxCatDescription.Text; string result = categoryBusiness.Add(category); MessageBox.Show(result); showCategories.ShowCategories(dataGridViewShowCats); }
public Category Add(Category category) { try { var bc = new CategoryBusiness(); return(bc.Add(category)); } catch (Exception ex) { var httpError = new HttpResponseMessage { StatusCode = (HttpStatusCode)422, ReasonPhrase = ex.Message }; throw new HttpResponseException(httpError); } }
public Category Add(Category category) { logger.Info("Start to add new cateory"); try { return(categoryBussiness.Add(category)); } catch (Exception ex) { var httpError = new HttpResponseMessage() { StatusCode = (HttpStatusCode)422, ReasonPhrase = ex.Message }; throw new HttpResponseException(httpError); } }
public void Add_ThrowsArgumentException_WhenCategoryWithGivenIdExists() { // Arrange int duplicateId = 1; var data = new List <Category> { new Category { Name = "TestCategory1", Id = 1 } }.AsQueryable(); var mockSet = new Mock <DbSet <Category> >(); mockSet.As <IQueryable <Category> >().Setup(m => m.Provider).Returns(data.Provider); mockSet.As <IQueryable <Category> >().Setup(m => m.Expression).Returns(data.Expression); mockSet.As <IQueryable <Category> >().Setup(m => m.ElementType).Returns(data.ElementType); mockSet.As <IQueryable <Category> >().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator()); var mockContext = new Mock <EazyCartContext>(); mockContext.Setup(c => c.Categories).Returns(mockSet.Object); mockContext.Setup(c => c.SaveChanges()).Throws(new Exception()); var categoryBusiness = new CategoryBusiness(mockContext.Object); // Act & Assert try { categoryBusiness.Add("TestCategory1", duplicateId); Assert.Fail("No exception was thrown"); } catch (ArgumentException ex) { string expectedMessage = string.Format($"Category with ID {duplicateId} already exists."); Assert.AreEqual(expectedMessage, ex.Message, "Wrong exception was thrown."); } }