public async Task Details_WithIncorrectSubcategoryId_ShouldShowErrorMessageAndReturnToHomeIndex() { const int nonExistingSubcategoryId = int.MaxValue; string errorMessage = null; //Arrange Mock <ISubcategoryService> subcategoryService = new Mock <ISubcategoryService>(); subcategoryService .Setup(s => s.IsSubcategoryExistingById(nonExistingSubcategoryId, false)) .ReturnsAsync(false); Mock <ITempDataDictionary> tempData = new Mock <ITempDataDictionary>(); tempData .SetupSet(t => t[TempDataErrorMessageKey] = It.IsAny <string>()) .Callback((string key, object message) => errorMessage = message as string); SubcategoriesController subcategoriesController = new SubcategoriesController(subcategoryService.Object) { TempData = tempData.Object }; //Act var result = await subcategoriesController.Details(nonExistingSubcategoryId, null); //Assert errorMessage.Should().Be(string.Format(EntityNotFound, SubcategoryEntity)); result.Should().BeOfType <RedirectToActionResult>(); result.As <RedirectToActionResult>().ActionName.Should().Be("Index"); result.As <RedirectToActionResult>().ControllerName.Should().Be("Home"); }
public async Task Index_WithCorrectPage_ShouldReturnViewResultWithValidViewModel() { const int page = 3; const int totalElements = 10; //Arrange Mock <IManagerSubcategoryService> managerSubcategoryService = new Mock <IManagerSubcategoryService>(); managerSubcategoryService .Setup(a => a.GetAllPagedListingAsync(false, searchToken, page)) .ReturnsAsync(new List <SubcategoryAdvancedServiceModel>()); managerSubcategoryService .Setup(a => a.TotalCountAsync(false, searchToken)) .ReturnsAsync(totalElements); SubcategoriesController subcategoriesController = new SubcategoriesController(managerSubcategoryService.Object, null, null); //Act var result = await subcategoriesController.Index(searchToken, false, page); //Assert result.Should().BeOfType <ViewResult>(); result.As <ViewResult>().Model.Should().BeOfType <PagingElementsViewModel <SubcategoryAdvancedServiceModel> >(); PagingElementsViewModel <SubcategoryAdvancedServiceModel> model = result.As <ViewResult>().Model.As <PagingElementsViewModel <SubcategoryAdvancedServiceModel> >(); model.SearchToken.Should().Be(searchToken); model.Pagination.CurrentPage.Should().Be(page); model.Pagination.PreviousPage.Should().Be(2); model.Pagination.NextPage.Should().Be(4); model.Pagination.TotalPages.Should().Be(4); model.Pagination.TotalElements.Should().Be(totalElements); model.Pagination.PageSize.Should().Be(SupplementPageSize); }
public async Task Restore_WithIncorrectSubcategoryId_ShouldReturnErrorMessageAndReturnToSubcategoriesIndex() { string errorMessage = null; //Arrange Mock <ISubcategoryService> subcategoryService = new Mock <ISubcategoryService>(); subcategoryService .Setup(s => s.IsSubcategoryExistingById(nonExistingSubcategoryId, true)) .ReturnsAsync(false); Mock <ITempDataDictionary> tempData = new Mock <ITempDataDictionary>(); tempData .SetupSet(t => t[TempDataErrorMessageKey] = It.IsAny <string>()) .Callback((string key, object message) => errorMessage = message as string); SubcategoriesController subcategoriesController = new SubcategoriesController(null, null, subcategoryService.Object) { TempData = tempData.Object }; //Act var result = await subcategoriesController.Restore(nonExistingSubcategoryId); //Assert errorMessage.Should().Be(string.Format(EntityNotFound, SubcategoryEntity)); result.Should().BeOfType <RedirectToActionResult>(); result.As <RedirectToActionResult>().ActionName.Should().Be("Index"); result.As <RedirectToActionResult>().ControllerName.Should().Be("Subcategories"); result.As <RedirectToActionResult>().RouteValues.Keys.Should().Contain("isDeleted"); result.As <RedirectToActionResult>().RouteValues.Values.Should().Contain(true); }
public async Task <IHttpActionResult> PostCategory([FromBody] Category category) { if (!ModelState.IsValid) { ModelState.AddModelError("Message", "The category details are not valid!"); return(BadRequest(ModelState)); } try { db.Categories.Add(category); await db.SaveChangesAsync(); Category lastcat = await db.Categories.OrderByDescending(catins => catins.categoryId).FirstAsync(); // add the dummy subcategory so the app does not fail Subcategory subcat = new Subcategory(); subcat.categoryId = lastcat.categoryId; subcat.subcategory = "Miscellaneous"; SubcategoriesController subctr = new SubcategoriesController(); await subctr.PostSubcategory(subcat); return(Ok <Category>(lastcat));; } catch (Exception) { ModelState.AddModelError("Message", "Error during saving your category!"); return(BadRequest(ModelState)); } }
public async Task Index_WithPageBiggerThanOneAndBiggerThanTotalPages_ShouldReturnToIndex() { const int page = 10; const int totalElements = 10; //Arrange Mock <IManagerSubcategoryService> managerSubcategoryService = new Mock <IManagerSubcategoryService>(); managerSubcategoryService .Setup(a => a.GetAllPagedListingAsync(false, searchToken, page)) .ReturnsAsync(new List <SubcategoryAdvancedServiceModel>()); managerSubcategoryService .Setup(a => a.TotalCountAsync(false, searchToken)) .ReturnsAsync(totalElements); SubcategoriesController subcategoriesController = new SubcategoriesController(managerSubcategoryService.Object, null, null); //Act var result = await subcategoriesController.Index(searchToken, false, page); //Assert result.Should().BeOfType <RedirectToActionResult>(); result.As <RedirectToActionResult>().ActionName.Should().Be("Index"); result.As <RedirectToActionResult>().RouteValues.Keys.Should().Contain("searchToken"); result.As <RedirectToActionResult>().RouteValues.Values.Should().Contain(searchToken); result.As <RedirectToActionResult>().RouteValues.Keys.Should().Contain("isDeleted"); result.As <RedirectToActionResult>().RouteValues.Values.Should().Contain(false); }
public void ShouldReturnSubcategoriesForCategoryId() { // Arrange var category = new Category { Name = "TestCategory", Id = 1 }; var categoryRepository = new StubICategoryRepository { GetSubcategoriesInt32 = categoryId => new List <Subcategory> { new Subcategory() { Name = "SubcategoryTest1", Id = 1, Category = category }, new Subcategory() { Name = "SubcategoryTest2", Id = 2, Category = category } } }; var controller = new SubcategoriesController(categoryRepository); SetupControllerForTests(controller); // Act var result = controller.GetSubcategories(1); var returnedSubcategories = new JavaScriptSerializer().Deserialize <ICollection <Subcategory> >(result.Content.ReadAsStringAsync().Result); // Assert Assert.AreEqual(result.StatusCode, HttpStatusCode.OK); Assert.AreEqual(2, returnedSubcategories.Count); }
public async Task EditPost_WithInvalidModelState_ShouldReturnValidViewModel() { //Arrange Mock <IManagerCategoryService> managerCategoryService = new Mock <IManagerCategoryService>(); managerCategoryService .Setup(m => m.GetAllBasicListingAsync(false)) .ReturnsAsync(new List <CategoryBasicServiceModel>()); SubcategoriesController subcategoriesController = new SubcategoriesController(null, managerCategoryService.Object, null); subcategoriesController.ModelState.AddModelError(string.Empty, "Error"); //Act var result = await subcategoriesController.Edit(subcategoryId, new SubcategoryFormViewModel()); //Assert result.Should().BeOfType <ViewResult>(); result.As <ViewResult>().Model.Should().BeOfType <SubcategoryFormViewModel>(); SubcategoryFormViewModel model = result.As <ViewResult>().Model.As <SubcategoryFormViewModel>(); model.CategoryId.Should().Be(0); model.Categories.Should().HaveCount(0); }
public async Task EditPost_WithExistingCategory_ShouldReturnErrorMessageAndReturnValidViewModel() { string errorMessage = null; //Arrange Mock <IManagerSubcategoryService> managerSubcategoryService = new Mock <IManagerSubcategoryService>(); managerSubcategoryService .Setup(m => m.IsSubcategoryModified(subcategoryId, null, categoryId)) .ReturnsAsync(true); Mock <IManagerCategoryService> managerCategoryService = new Mock <IManagerCategoryService>(); managerCategoryService .Setup(m => m.GetAllBasicListingAsync(false)) .ReturnsAsync(new List <CategoryBasicServiceModel>()); Mock <ISubcategoryService> subcategoryService = new Mock <ISubcategoryService>(); subcategoryService .Setup(s => s.IsSubcategoryExistingById(subcategoryId, false)) .ReturnsAsync(true); subcategoryService .Setup(s => s.IsSubcategoryExistingByIdAndName(subcategoryId, null)) .ReturnsAsync(true); Mock <ITempDataDictionary> tempData = new Mock <ITempDataDictionary>(); tempData .SetupSet(t => t[TempDataErrorMessageKey] = It.IsAny <string>()) .Callback((string key, object message) => errorMessage = message as string); SubcategoriesController subcategoriesController = new SubcategoriesController(managerSubcategoryService.Object, managerCategoryService.Object, subcategoryService.Object) { TempData = tempData.Object }; //Act var result = await subcategoriesController.Edit(subcategoryId, new SubcategoryFormViewModel() { CategoryId = categoryId }); //Assert errorMessage.Should().Be(string.Format(EntityExists, SubcategoryEntity)); result.Should().BeOfType <ViewResult>(); result.As <ViewResult>().Model.Should().BeOfType <SubcategoryFormViewModel>(); SubcategoryFormViewModel model = result.As <ViewResult>().Model.As <SubcategoryFormViewModel>(); model.CategoryId.Should().Be(categoryId); model.Categories.Should().HaveCount(0); }
public async Task EditPost_WithCorrectData_ShouldReturnSuccessMessageAndReturnToSubcategoriesIndex() { string successMessage = null; //Arrange Mock <IManagerSubcategoryService> managerSubcategoryService = new Mock <IManagerSubcategoryService>(); managerSubcategoryService .Setup(m => m.IsSubcategoryModified(subcategoryId, null, categoryId)) .ReturnsAsync(true); managerSubcategoryService .Setup(m => m.EditAsync(subcategoryId, null, categoryId)) .Returns(Task.CompletedTask); Mock <ISubcategoryService> subcategoryService = new Mock <ISubcategoryService>(); subcategoryService .Setup(s => s.IsSubcategoryExistingById(subcategoryId, false)) .ReturnsAsync(true); subcategoryService .Setup(s => s.IsSubcategoryExistingByIdAndName(subcategoryId, null)) .ReturnsAsync(false); Mock <ITempDataDictionary> tempData = new Mock <ITempDataDictionary>(); tempData .SetupSet(t => t[TempDataSuccessMessageKey] = It.IsAny <string>()) .Callback((string key, object message) => successMessage = message as string); SubcategoriesController subcategoriesController = new SubcategoriesController(managerSubcategoryService.Object, null, subcategoryService.Object) { TempData = tempData.Object }; //Act var result = await subcategoriesController.Edit(subcategoryId, new SubcategoryFormViewModel() { CategoryId = categoryId }); //Assert successMessage.Should().Be(string.Format(EntityModified, SubcategoryEntity)); result.Should().BeOfType <RedirectToActionResult>(); result.As <RedirectToActionResult>().ActionName.Should().Be("Index"); result.As <RedirectToActionResult>().ControllerName.Should().Be("Subcategories"); result.As <RedirectToActionResult>().RouteValues.Keys.Should().Contain("isDeleted"); result.As <RedirectToActionResult>().RouteValues.Values.Should().Contain(false); }
public async Task EditGet_WithCorrectSubcategoryId_ShouldReturnValidViewModel() { string errorMessage = null; //Arrange Mock <IManagerSubcategoryService> managerSubcategoryService = new Mock <IManagerSubcategoryService>(); managerSubcategoryService .Setup(m => m.GetEditModelAsync(subcategoryId)) .ReturnsAsync(new SubcategoryBasicServiceModel() { Id = subcategoryId }); Mock <IManagerCategoryService> managerCategoryService = new Mock <IManagerCategoryService>(); managerCategoryService .Setup(m => m.GetAllBasicListingAsync(false)) .ReturnsAsync(new List <CategoryBasicServiceModel>()); Mock <ISubcategoryService> subcategoryService = new Mock <ISubcategoryService>(); subcategoryService .Setup(s => s.IsSubcategoryExistingById(subcategoryId, false)) .ReturnsAsync(true); subcategoryService .Setup(s => s.GetCategoryIdBySubcategoryId(subcategoryId)) .ReturnsAsync(categoryId); Mock <ITempDataDictionary> tempData = new Mock <ITempDataDictionary>(); tempData .SetupSet(t => t[TempDataErrorMessageKey] = It.IsAny <string>()) .Callback((string key, object message) => errorMessage = message as string); SubcategoriesController subcategoriesController = new SubcategoriesController(managerSubcategoryService.Object, managerCategoryService.Object, subcategoryService.Object) { TempData = tempData.Object }; //Act var result = await subcategoriesController.Edit(subcategoryId); //Assert result.Should().BeOfType <ViewResult>(); result.As <ViewResult>().Model.Should().BeOfType <SubcategoryFormViewModel>(); }
public void ShouldReturnNotFoundForSubcategories() { // Arrange var categoryRepository = new StubICategoryRepository { GetSubcategoriesInt32 = categoryId => null }; var controller = new SubcategoriesController(categoryRepository); SetupControllerForTests(controller); // Act var result = controller.GetSubcategories(1); // Assert Assert.AreEqual(result.StatusCode, HttpStatusCode.NotFound); }
public async Task CreatePost_WithCorrectData_ShouldReturnSuccessMessageAndReturnToSubcategoriesIndex() { string successMessage = null; //Arrange Mock <IManagerCategoryService> managerCategoryService = new Mock <IManagerCategoryService>(); managerCategoryService .Setup(m => m.GetAllBasicListingAsync(false)) .ReturnsAsync(new List <CategoryBasicServiceModel>()); Mock <IManagerSubcategoryService> managerSubcategoryService = new Mock <IManagerSubcategoryService>(); managerSubcategoryService .Setup(m => m.CreateAsync(null, 0)) .Returns(Task.CompletedTask); Mock <ISubcategoryService> subcategoryService = new Mock <ISubcategoryService>(); subcategoryService .Setup(c => c.IsSubcategoryExistingByName(null)) .ReturnsAsync(false); Mock <ITempDataDictionary> tempData = new Mock <ITempDataDictionary>(); tempData .SetupSet(t => t[TempDataSuccessMessageKey] = It.IsAny <string>()) .Callback((string key, object message) => successMessage = message as string); SubcategoriesController subcategoriesController = new SubcategoriesController(managerSubcategoryService.Object, managerCategoryService.Object, subcategoryService.Object) { TempData = tempData.Object }; //Act var result = await subcategoriesController.Create(new SubcategoryFormViewModel()); //Assert successMessage.Should().Be(string.Format(EntityCreated, SubcategoryEntity)); result.Should().BeOfType <RedirectToActionResult>(); result.As <RedirectToActionResult>().ActionName.Should().Be("Index"); result.As <RedirectToActionResult>().ControllerName.Should().Be("Subcategories"); result.As <RedirectToActionResult>().RouteValues.Keys.Should().Contain("isDeleted"); result.As <RedirectToActionResult>().RouteValues.Values.Should().Contain(false); }
public async Task Details_WithCorrectSubcategoryIdAndCorrectPage_ShouldReturnValidPaginationModelAndValidViewModel() { const int page = 3; const int totalElements = 10; //Arrange Mock <ISubcategoryService> subcategoryService = new Mock <ISubcategoryService>(); subcategoryService .Setup(s => s.IsSubcategoryExistingById(subcategoryId, false)) .ReturnsAsync(true); subcategoryService .Setup(s => s.GetDetailsByIdAsync(subcategoryId, page)) .ReturnsAsync(new SubcategoryDetailsServiceModel { Supplements = new List <SupplementAdvancedServiceModel> { new SupplementAdvancedServiceModel { } } }); subcategoryService .Setup(s => s.TotalSupplementsCountAsync(subcategoryId)) .ReturnsAsync(totalElements); SubcategoriesController subcategoriesController = new SubcategoriesController(subcategoryService.Object); //Act var result = await subcategoriesController.Details(subcategoryId, subcategoryName, page); //Assert result.Should().BeOfType <ViewResult>(); result.As <ViewResult>().ViewData.Should().ContainKey("ReturnUrl"); result.As <ViewResult>().ViewData.Should().ContainValue($"/subcategories/details/{subcategoryId}?name={subcategoryName}"); result.As <ViewResult>().Model.Should().BeOfType <PagingElementViewModel <SubcategoryDetailsServiceModel> >(); PagingElementViewModel <SubcategoryDetailsServiceModel> model = result.As <ViewResult>().Model.As <PagingElementViewModel <SubcategoryDetailsServiceModel> >(); model.Element.Supplements.Should().HaveCount(1); model.Pagination.CurrentPage.Should().Be(page); model.Pagination.PreviousPage.Should().Be(2); model.Pagination.NextPage.Should().Be(4); model.Pagination.TotalPages.Should().Be(4); model.Pagination.TotalElements.Should().Be(totalElements); model.Pagination.PageSize.Should().Be(SupplementPageSize); }
public async Task Index_WithPageLessThanOneOrEqualToZero_ShouldReturnToIndex(int page) { //Arrange SubcategoriesController subcategoriesController = new SubcategoriesController(null, null, null); //Act var result = await subcategoriesController.Index(searchToken, false, page); //Assert result.Should().BeOfType <RedirectToActionResult>(); result.As <RedirectToActionResult>().ActionName.Should().Be("Index"); result.As <RedirectToActionResult>().RouteValues.Keys.Should().Contain("searchToken"); result.As <RedirectToActionResult>().RouteValues.Values.Should().Contain(searchToken); result.As <RedirectToActionResult>().RouteValues.Keys.Should().Contain("isDeleted"); result.As <RedirectToActionResult>().RouteValues.Values.Should().Contain(false); }
public async Task CreateGet_ShouldReturnValidViewModel() { //Arrange Mock <IManagerCategoryService> managerCategoryService = new Mock <IManagerCategoryService>(); managerCategoryService .Setup(m => m.GetAllBasicListingAsync(false)) .ReturnsAsync(new List <CategoryBasicServiceModel>()); SubcategoriesController subcategoriesController = new SubcategoriesController(null, managerCategoryService.Object, null); //Act var result = await subcategoriesController.Create(); //Assert result.Should().BeOfType <ViewResult>(); result.As <ViewResult>().Model.Should().BeOfType <SubcategoryFormViewModel>(); }
public async Task Details_WithCorrectSubcategoryIdAndPageBiggerThanTotalPages_ShouldReturnToDetails() { const int page = 10; const int totalElements = 10; //Arrange Mock <ISubcategoryService> subcategoryService = new Mock <ISubcategoryService>(); subcategoryService .Setup(s => s.IsSubcategoryExistingById(subcategoryId, false)) .ReturnsAsync(true); subcategoryService .Setup(s => s.GetDetailsByIdAsync(subcategoryId, page)) .ReturnsAsync(new SubcategoryDetailsServiceModel { Supplements = new List <SupplementAdvancedServiceModel> { new SupplementAdvancedServiceModel { } } }); subcategoryService .Setup(s => s.TotalSupplementsCountAsync(subcategoryId)) .ReturnsAsync(totalElements); SubcategoriesController subcategoriesController = new SubcategoriesController(subcategoryService.Object); //Act var result = await subcategoriesController.Details(subcategoryId, subcategoryName, page); //Assert result.Should().BeOfType <RedirectToActionResult>(); result.As <RedirectToActionResult>().ActionName.Should().Be("Details"); result.As <RedirectToActionResult>().RouteValues.Keys.Should().Contain("id"); result.As <RedirectToActionResult>().RouteValues.Values.Should().Contain(subcategoryId); result.As <RedirectToActionResult>().RouteValues.Keys.Should().Contain("name"); result.As <RedirectToActionResult>().RouteValues.Values.Should().Contain(subcategoryName); }
public async Task CreatePost_WithExistingSubcategory_ShouldReturnErrorMessageAndReturnValidViewModel() { string errorMessage = null; //Arrange Mock <IManagerCategoryService> managerCategoryService = new Mock <IManagerCategoryService>(); managerCategoryService .Setup(m => m.GetAllBasicListingAsync(false)) .ReturnsAsync(new List <CategoryBasicServiceModel>()); Mock <ISubcategoryService> subcategoryService = new Mock <ISubcategoryService>(); subcategoryService .Setup(c => c.IsSubcategoryExistingByName(null)) .ReturnsAsync(true); Mock <ITempDataDictionary> tempData = new Mock <ITempDataDictionary>(); tempData .SetupSet(t => t[TempDataErrorMessageKey] = It.IsAny <string>()) .Callback((string key, object message) => errorMessage = message as string); SubcategoriesController subcategoriesController = new SubcategoriesController(null, managerCategoryService.Object, subcategoryService.Object) { TempData = tempData.Object }; //Act var result = await subcategoriesController.Create(new SubcategoryFormViewModel()); //Assert errorMessage.Should().Be(string.Format(EntityExists, SubcategoryEntity)); result.Should().BeOfType <ViewResult>(); result.As <ViewResult>().Model.Should().BeOfType <SubcategoryFormViewModel>(); }
public async Task Details_WithCorrectSubcategoryIdAndPageLessThanOneOrEqualToZero_ShouldReturnToDetails(int page) { //Arrange Mock <ISubcategoryService> subcategoryService = new Mock <ISubcategoryService>(); subcategoryService .Setup(s => s.IsSubcategoryExistingById(subcategoryId, false)) .ReturnsAsync(true); SubcategoriesController subcategoriesController = new SubcategoriesController(subcategoryService.Object); //Act var result = await subcategoriesController.Details(subcategoryId, subcategoryName, page); //Assert result.Should().BeOfType <RedirectToActionResult>(); result.As <RedirectToActionResult>().ActionName.Should().Be("Details"); result.As <RedirectToActionResult>().RouteValues.Keys.Should().Contain("id"); result.As <RedirectToActionResult>().RouteValues.Values.Should().Contain(subcategoryId); result.As <RedirectToActionResult>().RouteValues.Keys.Should().Contain("name"); result.As <RedirectToActionResult>().RouteValues.Values.Should().Contain(subcategoryName); }