public void AddCategory_DuplicateCategory_Unsuccess() { //setup mocks Mock <ICategoriesRepository> mockRepo = new Mock <ICategoriesRepository>(); Mock <IMapper> mockMapper = new Mock <IMapper>(); Category category = new Category { Name = "Testowa" }; CategoryDTO categoryDTO = new CategoryDTO { Name = "Testowa" }; CategoriesService service = new CategoriesService(mockRepo.Object, mockMapper.Object); mockMapper.Setup(m => m.Map <CategoryDTO, Category>(categoryDTO)).Returns(category); service.AddCategory(categoryDTO); mockRepo.Setup(m => m.GetCategoryByName(categoryDTO.Name)).Returns(category); //act and asserts Assert.Throws <ValidationException>(() => { service.AddCategory(categoryDTO); }); }
public ActionResult AddCategory(string name) { CategoriesService service = new CategoriesService(); int id = service.AddCategory(name); return(RedirectToAction("CategoriesSelectList", new { selectedId = id })); }
public void AddCategory_ValidCategory_Success() { //arrange Mock <ICategoriesRepository> mockRepo = new Mock <ICategoriesRepository>(); Mock <IMapper> mockMapper = new Mock <IMapper>(); Category category = new Category { Name = "Testowa" }; CategoryDTO categoryDTO = new CategoryDTO { Name = "Testowa" }; CategoriesService service = new CategoriesService(mockRepo.Object, mockMapper.Object); mockMapper.Setup(m => m.Map <Category>(categoryDTO)).Returns(category); mockRepo.Setup(m => m.GetCategoryByName(categoryDTO.Name)).Returns((Category)null); //act service.AddCategory(categoryDTO); //asserts mockRepo.Verify(m => m.Add(category)); }
public async Task AddCategoryAsync() { string error = _dataValidation.CategoryDataValidation(NewCategory); if (error == null) { var service = new CategoriesService(); error = await service.AddCategory(NewCategory); } ShowMessageDialog(error); }
public ActionResult <Category> Post([FromBody] Category categoryData) { try { Category newCategory = _cs.AddCategory(categoryData); return(Created("api/category" + newCategory.Id, newCategory)); } catch (Exception e) { return(BadRequest(e.Message)); } }
public IActionResult Post(Category category) { try { _categoriesService.AddCategory(category); return(new StatusCodeResult(201)); } catch { return(BadRequest()); } }
public ActionResult <Category> Create([FromBody] Category category) { try { Category postedCategory = _cs.AddCategory(category); return(Created("api/questions/" + postedCategory.Id, postedCategory)); } catch (Exception e) { return(BadRequest(e.Message)); } }
public void ShouldThrowArgumentNullException_WhenNullCategoryIsPassed() { // Arrange var mockedUnitOfWork = new Mock <IUnitOfWork>(); var mockedCategoryRepository = new Mock <IGenericRepository <Category> >(); var mockedProductRepository = new Mock <IGenericRepository <Product> >(); var categoriesService = new CategoriesService(mockedUnitOfWork.Object, mockedCategoryRepository.Object, mockedProductRepository.Object); // Act and Assert Assert.Throws <ArgumentNullException>(() => categoriesService.AddCategory(null)); }
public void ShouldCallCommitMethodOfUnitOfWorkOnce_WhenACategoryIsPassed() { // Arrange var category = new Mock <Category>(); var mockedUnitOfWork = new Mock <IUnitOfWork>(); var mockedCategoryRepository = new Mock <IGenericRepository <Category> >(); var mockedProductRepository = new Mock <IGenericRepository <Product> >(); var categoriesService = new CategoriesService(mockedUnitOfWork.Object, mockedCategoryRepository.Object, mockedProductRepository.Object); // Act categoriesService.AddCategory(category.Object); // Assert mockedUnitOfWork.Verify(uow => uow.Commit(), Times.Once); }
public void ShouldThrowArgumentNullExceptionWithCorrectMessage_WhenNullCategoryIsPassed() { // Arrange var expectedExMessage = "Category cannot be null."; var mockedUnitOfWork = new Mock <IUnitOfWork>(); var mockedCategoryRepository = new Mock <IGenericRepository <Category> >(); var mockedProductRepository = new Mock <IGenericRepository <Product> >(); var categoriesService = new CategoriesService(mockedUnitOfWork.Object, mockedCategoryRepository.Object, mockedProductRepository.Object); // Act and Assert var exception = Assert.Throws <ArgumentNullException>(() => categoriesService.AddCategory(null)); StringAssert.Contains(expectedExMessage, exception.Message); }
public void ShouldCallAddMethodOfCategoryRepositoryOnce_WhenACategoryIsPassed() { // Arrange var category = new Mock <Category>(); var mockedUnitOfWork = new Mock <IUnitOfWork>(); var mockedCategoryRepository = new Mock <IGenericRepository <Category> >(); mockedCategoryRepository.Setup(cr => cr.Add(It.IsAny <Category>())).Verifiable(); var mockedProductRepository = new Mock <IGenericRepository <Product> >(); var categoriesService = new CategoriesService(mockedUnitOfWork.Object, mockedCategoryRepository.Object, mockedProductRepository.Object); // Act categoriesService.AddCategory(category.Object); // Assert mockedCategoryRepository.Verify(cs => cs.Add(category.Object), Times.Once); }
public void AddCategory_ShouldReturn_Success() { // Arrange CategoriesModel category = new CategoriesModel { Name = "New category", SupplierId = 2, Link = "link", Rate = 1.5m, Notes = "notes" }; fakeCategoriesRepository.Setup(a => a.Add(category)); categoriesService = new CategoriesService(fakeCategoriesRepository.Object, new Mock <ICommonRepository>().Object); CategoriesDtoModel categoriesDto = new CategoriesDtoModel { Name = category.Name, SupplierId = category.SupplierId, SupplierName = "Supplier", Link = category.Link, Rate = 1.5m, Notes = category.Notes }; try { // Act categoriesService.AddCategory(categoriesDto); operationSucceeded = true; } catch (Exception ex) { errorMessage = ex.Message + " | " + ex.StackTrace; } // Assert Assert.IsTrue(operationSucceeded, errorMessage); }