Beispiel #1
0
        public void RedirectToActionIndex_WithTheCorrectModel__WhenModelStateIsValid()
        {
            //Arrange
            var foodCategoriesServiceMock = new Mock <IFoodCategoriesService>();
            var mappingServiceMock        = new Mock <IMappingService>();
            var controller = new FoodCategoriesController(foodCategoriesServiceMock.Object, mappingServiceMock.Object);

            AddFoodCategoryViewModel foodCategoryModel = new AddFoodCategoryViewModel();

            foodCategoryModel.Name = "Tomatos";
            var foodcategoryId = Guid.NewGuid();

            foodCategoryModel.FoodType      = FoodType.Cheese;
            foodCategoryModel.MeasuringUnit = MeasuringUnitType.Kg;

            var validationContext = new ValidationContext(foodCategoryModel, null, null);
            var validationResults = new List <ValidationResult>();

            Validator.TryValidateObject(foodCategoryModel, validationContext, validationResults, true);
            foreach (var validationResult in validationResults)
            {
                controller.ModelState.AddModelError(validationResult.MemberNames.First(), validationResult.ErrorMessage);
            }

            //Act & Assert
            controller.WithCallTo(x => x.AddFoodCategory(foodCategoryModel))
            .ShouldRedirectTo(x => x.Index());
        }
        public ActionResult AddFoodCategory(AddFoodCategoryViewModel model)
        {
            if (!this.ModelState.IsValid)
            {
                this.AddToastMessage(toastrFailureTitle, string.Format(toastrAddObjectFailureMessage, model.Name), ToastType.Error);
                return(this.View(model));
            }

            var foodCategory = this.mappingService.Map <FoodCategory>(model);

            this.foodCategoriesService.AddFoodCategory(foodCategory);

            this.AddToastMessage(toastrSuccessTitle, string.Format(toastrAddObjectSuccessMessage, model.Name), ToastType.Success);
            return(this.RedirectToAction("Index", "FoodCategories"));
        }