Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        public async Task EditGet_WithIncorrectSubcategoryId_ShouldReturnErrorMessageAndReturnToSubcategoriesIndex()
        {
            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(null, null, subcategoryService.Object)
            {
                TempData = tempData.Object
            };

            //Act
            var result = await subcategoriesController.Edit(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(false);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
        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>();
        }