예제 #1
0
        public void RenderTheRightView_EditRecipe_WhenValidGuidIdIsPassed()
        {
            //Arrange
            Guid id     = Guid.NewGuid();
            var  recipe = new Recipe()
            {
                Id          = id,
                Title       = "Tomato Salad",
                DishType    = DishType.Salad,
                Describtion = "Some describtion",
                Instruction = "These are the instructions"
            };
            var ingredientsServiceMock    = new Mock <IIngredientsService>();
            var foodCategoriesServiceMock = new Mock <IFoodCategoriesService>();
            var recipesServiceMock        = new Mock <IRecipesService>();
            var mappingServiceMock        = new Mock <IMappingService>();
            var controller = new RecipesController(recipesServiceMock.Object, ingredientsServiceMock.Object, foodCategoriesServiceMock.Object, mappingServiceMock.Object);

            recipesServiceMock.Setup(x => x.GetRecipeById(id)).Returns(recipe);

            var model = new RecipeViewModel()
            {
                Title       = recipe.Title,
                DishType    = recipe.DishType,
                Instruction = recipe.Instruction,
                Describtion = recipe.Describtion
            };

            mappingServiceMock.Setup(x => x.Map <RecipeViewModel>(recipe)).Returns(model);

            //Act & Assert
            controller.WithCallTo(x => x.EditRecipe(id))
            .ShouldRenderView("EditRecipe")
            .WithModel(model);
        }
예제 #2
0
        public void ReturnJsonResultWithIngredientsAndRightContent()
        {
            //Arrange
            var ingredientsServiceMock    = new Mock <IIngredientsService>();
            var foodCategoriesServiceMock = new Mock <IFoodCategoriesService>();
            var recipesServiceMock        = new Mock <IRecipesService>();
            var mappingServiceMock        = new Mock <IMappingService>();
            var controller    = new RecipesController(recipesServiceMock.Object, ingredientsServiceMock.Object, foodCategoriesServiceMock.Object, mappingServiceMock.Object);
            var id            = Guid.NewGuid();
            var name          = "Tomatos";
            var foodType      = FoodType.Vegetable;
            var measuringUnit = MeasuringUnitType.Kg;

            var foodcategory = new FoodCategory()
            {
                Id            = id,
                Name          = name,
                FoodType      = foodType,
                MeasuringUnit = measuringUnit
            };
            Guid    ingredientId            = Guid.NewGuid();
            string  ingredientName          = "tomato";
            decimal pricePerMeasuringUnit   = 1.20m;
            double  quantityInMeasuringUnit = 0.250;

            Ingredient ingredient = new Ingredient()
            {
                Id                      = ingredientId,
                Name                    = ingredientName,
                FoodCategory            = foodcategory,
                PricePerMeasuringUnit   = pricePerMeasuringUnit,
                QuantityInMeasuringUnit = quantityInMeasuringUnit
            };

            IngredientViewModel ingredientModel = new IngredientViewModel()
            {
                Id                      = ingredientId,
                Name                    = ingredientName,
                FoodCategory            = foodcategory,
                PricePerMeasuringUnit   = pricePerMeasuringUnit,
                QuantityInMeasuringUnit = quantityInMeasuringUnit
            };

            var ingredients = new List <IngredientViewModel>()
            {
                ingredientModel
            };

            ingredientsServiceMock.Setup(x => x.GetAllIngredients()).Returns(new List <Ingredient> {
                ingredient
            });
            string query = "tomato";

            //Act & Assert
            controller.WithCallTo(x => x.AutoComplete(query))
            .ShouldReturnJson(data =>
            {
                Assert.That(ingredients[0].Name, Is.EqualTo(ingredientName));
            });
        }
        public void ReturnJsonResultWithFoodCategoies()
        {
            //Arrange
            var ingredientsServiceMock    = new Mock <IIngredientsService>();
            var foodCategoriesServiceMock = new Mock <IFoodCategoriesService>();
            var recipesServiceMock        = new Mock <IRecipesService>();
            var mappingServiceMock        = new Mock <IMappingService>();
            var controller = new RecipesController(recipesServiceMock.Object, ingredientsServiceMock.Object, foodCategoriesServiceMock.Object, mappingServiceMock.Object);

            var id            = Guid.NewGuid();
            var name          = "Tomatos";
            var foodType      = FoodType.Vegetable;
            var measuringUnit = MeasuringUnitType.Kg;

            var foodcategory = new FoodCategory()
            {
                Id            = id,
                Name          = name,
                FoodType      = foodType,
                MeasuringUnit = measuringUnit
            };

            var foodCategories = new List <FoodCategory>()
            {
                foodcategory
            };

            foodCategoriesServiceMock.Setup(x => x.GetAllFoodCategories()).Returns(foodCategories);

            //Act & Assert
            controller.WithCallTo(x => x.GetFoodCategories())
            .ShouldReturnJson();
        }
예제 #4
0
        public void RedirectToActionIndex_WithTheCorrectModel__WhenModelStateIsValid()
        {
            //Arrange
            var ingredientsServiceMock    = new Mock <IIngredientsService>();
            var foodCategoriesServiceMock = new Mock <IFoodCategoriesService>();
            var recipesServiceMock        = new Mock <IRecipesService>();
            var mappingServiceMock        = new Mock <IMappingService>();
            var controller = new RecipesController(recipesServiceMock.Object, ingredientsServiceMock.Object, foodCategoriesServiceMock.Object, mappingServiceMock.Object);

            Guid id     = Guid.NewGuid();
            var  recipe = new Recipe()
            {
                Id          = id,
                Title       = "Tomato Salad",
                DishType    = DishType.Salad,
                Instruction = "These are the instructions",
                Describtion = "The describtion of tge recipe is here"
            };
            var model = new RecipeViewModel()
            {
                Title       = recipe.Title,
                DishType    = recipe.DishType,
                Instruction = recipe.Instruction,
                Describtion = recipe.Describtion
            };

            mappingServiceMock.Setup(x => x.Map <RecipeViewModel>(recipe)).Returns(model);

            //Act & Assert
            controller.WithCallTo(x => x.EditRecipe(model))
            .ShouldRedirectTo(x => x.Index());
        }
        public void RenderTheRightView_DetailsRecipeWithModel_RecipeViewModel_When_IdGuidIsValid()
        {
            //Arrange
            var ingredientsServiceMock    = new Mock <IIngredientsService>();
            var foodCategoriesServiceMock = new Mock <IFoodCategoriesService>();
            var recipesServiceMock        = new Mock <IRecipesService>();
            var mappingServiceMock        = new Mock <IMappingService>();
            var controller = new RecipesController(recipesServiceMock.Object, ingredientsServiceMock.Object, foodCategoriesServiceMock.Object, mappingServiceMock.Object);

            Guid id     = Guid.NewGuid();
            var  recipe = new Recipe()
            {
                Id          = id,
                Title       = "Tomato Salad",
                DishType    = DishType.Salad,
                Instruction = "These are the instructions",
                Describtion = "The describtion of tge recipe is here"
            };
            var model = new RecipeViewModel()
            {
                Title       = recipe.Title,
                DishType    = recipe.DishType,
                Instruction = recipe.Instruction,
                Describtion = recipe.Describtion
            };

            recipesServiceMock.Setup(x => x.GetRecipeById(id)).Returns(recipe);
            mappingServiceMock.Setup(x => x.Map <RecipeViewModel>(recipe)).Returns(model);

            //Act & Assert
            controller.WithCallTo(x => x.DetailsRecipe(id))
            .ShouldRenderView("DetailsRecipe")
            .WithModel(model)
            .AndNoModelErrors();
        }
        public void RedirectToActionIndex__WhenRecipeIsSuccessfullyDeleted()
        {
            //Arrange
            var ingredientsServiceMock    = new Mock <IIngredientsService>();
            var foodCategoriesServiceMock = new Mock <IFoodCategoriesService>();
            var recipesServiceMock        = new Mock <IRecipesService>();
            var mappingServiceMock        = new Mock <IMappingService>();
            var controller = new RecipesController(recipesServiceMock.Object, ingredientsServiceMock.Object, foodCategoriesServiceMock.Object, mappingServiceMock.Object);
            var id         = Guid.NewGuid();

            var recipe = new Recipe()
            {
                Id          = id,
                Title       = "Tomato Salad",
                DishType    = DishType.Salad,
                Instruction = "These are the instructions",
                Describtion = "The describtion of tge recipe is here"
            };

            recipesServiceMock.Setup(x => x.GetRecipeById(id)).Returns(recipe);
            recipesServiceMock.Setup(x => x.DeleteRecipe(recipe));

            //Act & Assert
            controller.WithCallTo(x => x.DeleteRecipeConfirm(id))
            .ShouldRedirectTo(x => x.Index());
        }
        public void RedirectToActionIndex_WithTheCorrectModel__WhenModelStateIsValid()
        {
            //Arrange
            var ingredientsServiceMock    = new Mock <IIngredientsService>();
            var foodCategoriesServiceMock = new Mock <IFoodCategoriesService>();
            var recipesServiceMock        = new Mock <IRecipesService>();
            var mappingServiceMock        = new Mock <IMappingService>();
            var controller = new RecipesController(recipesServiceMock.Object, ingredientsServiceMock.Object, foodCategoriesServiceMock.Object, mappingServiceMock.Object);

            Guid recipeId       = Guid.NewGuid();
            Guid foodCategoryId = Guid.NewGuid();
            IEnumerable <AddIngredientViewModel> ingredients = new List <AddIngredientViewModel>()
            {
                new AddIngredientViewModel()
                {
                    Name = "Blueberries"
                }
            };
            AddRecipeViewModel model = new AddRecipeViewModel()
            {
                Title       = null,
                Describtion = "A long describtion",
                Ingredients = ingredients
            };
            IEnumerable <string>  ingredientNames      = new List <string>();
            IEnumerable <double>  ingredientQuantities = new List <double>();
            IEnumerable <decimal> ingredientPrices     = new List <decimal>();
            IEnumerable <Guid>    foodCategories       = new List <Guid>();


            //Act & Assert
            controller.WithCallTo(x => x.AddRecipe(model, ingredientNames, ingredientQuantities, ingredientPrices, foodCategories))
            .ShouldRedirectTo(x => x.Index());
        }
        public void RenderTheRightView_AddRecipe()
        {
            //Arrange
            var ingredientsServiceMock    = new Mock <IIngredientsService>();
            var foodCategoriesServiceMock = new Mock <IFoodCategoriesService>();
            var recipesServiceMock        = new Mock <IRecipesService>();
            var mappingServiceMock        = new Mock <IMappingService>();
            var controller = new RecipesController(recipesServiceMock.Object, ingredientsServiceMock.Object, foodCategoriesServiceMock.Object, mappingServiceMock.Object);

            //Act & Assert
            controller.WithCallTo(x => x.AddRecipe())
            .ShouldRenderView("AddRecipe");
        }
예제 #9
0
        public void RedirectToErrorPage_When_IdGuidIsNotValid()
        {
            //Arrange
            var  ingredientsServiceMock    = new Mock <IIngredientsService>();
            var  foodCategoriesServiceMock = new Mock <IFoodCategoriesService>();
            var  recipesServiceMock        = new Mock <IRecipesService>();
            var  mappingServiceMock        = new Mock <IMappingService>();
            var  controller = new RecipesController(recipesServiceMock.Object, ingredientsServiceMock.Object, foodCategoriesServiceMock.Object, mappingServiceMock.Object);
            Guid id         = Guid.Empty;

            //Act & Assert
            controller.WithCallTo(x => x.EditRecipe(id))
            .ShouldRenderView("404.html");
        }
예제 #10
0
        public void RenderTheRightView()
        {
            //Arrange
            IEnumerable <Ingredient> ingredients = new List <Ingredient>();
            var inredientsServiceMock            = new Mock <IIngredientsService>();

            inredientsServiceMock.Setup(x => x.GetAllIngredients()).Returns(ingredients);
            var foodCategoriesServiceMock = new Mock <IFoodCategoriesService>();
            var recipesServiceMock        = new Mock <IRecipesService>();
            var mappingServiceMock        = new Mock <IMappingService>();
            var controller = new RecipesController(recipesServiceMock.Object, inredientsServiceMock.Object, foodCategoriesServiceMock.Object, mappingServiceMock.Object);

            //Act & Assert
            controller.WithCallTo(x => x.Index())
            .ShouldRenderView("Index");
        }
        public void RenderTheRightView_DeleteRecipe__WhenRecipeWasNotFoundInDatabase()
        {
            //Arrange
            var ingredientsServiceMock    = new Mock <IIngredientsService>();
            var foodCategoriesServiceMock = new Mock <IFoodCategoriesService>();
            var recipesServiceMock        = new Mock <IRecipesService>();
            var mappingServiceMock        = new Mock <IMappingService>();
            var controller = new RecipesController(recipesServiceMock.Object, ingredientsServiceMock.Object, foodCategoriesServiceMock.Object, mappingServiceMock.Object);
            var id         = Guid.NewGuid();

            recipesServiceMock.Setup(x => x.GetRecipeById(id)).Returns <Recipe>(null);

            //Act & Assert
            controller.WithCallTo(x => x.DeleteRecipe(id))
            .ShouldRenderView("DeleteRecipe");
        }
예제 #12
0
        public void RenderTheRightPartialView_RecipesGridPartial()
        {
            //Arrange
            IEnumerable <Ingredient> ingredients = new List <Ingredient>();
            var inredientsServiceMock            = new Mock <IIngredientsService>();

            inredientsServiceMock.Setup(x => x.GetAllIngredients()).Returns(ingredients);
            var    foodCategoriesServiceMock = new Mock <IFoodCategoriesService>();
            var    recipesServiceMock        = new Mock <IRecipesService>();
            var    mappingServiceMock        = new Mock <IMappingService>();
            var    controller = new RecipesController(recipesServiceMock.Object, inredientsServiceMock.Object, foodCategoriesServiceMock.Object, mappingServiceMock.Object);
            string title      = "Backed Potatoes";

            //Act & Assert
            controller.WithCallTo(x => x.Search(title))
            .ShouldRenderPartialView("_RecipesGridPartial");
        }
        public void RenderTheRightView_AddRecipe_WithTheCorrectModel_RecipeViewModel_WhenModelStateIsNotValid()
        {
            //Arrange
            var  ingredientsServiceMock    = new Mock <IIngredientsService>();
            var  foodCategoriesServiceMock = new Mock <IFoodCategoriesService>();
            var  recipesServiceMock        = new Mock <IRecipesService>();
            var  mappingServiceMock        = new Mock <IMappingService>();
            var  controller     = new RecipesController(recipesServiceMock.Object, ingredientsServiceMock.Object, foodCategoriesServiceMock.Object, mappingServiceMock.Object);
            Guid recipeId       = Guid.NewGuid();
            Guid foodCategoryId = Guid.NewGuid();
            IEnumerable <AddIngredientViewModel> ingredients = new List <AddIngredientViewModel>()
            {
                new AddIngredientViewModel()
                {
                    Name = "Blueberries"
                }
            };
            AddRecipeViewModel model = new AddRecipeViewModel()
            {
                Title       = null,
                Describtion = "A long describtion",
                Ingredients = ingredients
            };
            IEnumerable <string>  ingredientNames      = new List <string>();
            IEnumerable <double>  ingredientQuantities = new List <double>();
            IEnumerable <decimal> ingredientPrices     = new List <decimal>();
            IEnumerable <Guid>    foodCategories       = new List <Guid>();

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

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

            //Act & Assert
            controller.WithCallTo(x => x.AddRecipe(model, ingredientNames, ingredientQuantities, ingredientPrices, foodCategories))
            .ShouldRenderView("AddRecipe")
            .WithModel <AddRecipeViewModel>()
            .AndModelError("Instruction");
        }
예제 #14
0
        public void RenderTheRightView_EditRecipe_WithTheCorrectModel_RecipeViewModel_WhenModelStateIsNotValid()
        {
            //Arrange
            var  ingredientsServiceMock    = new Mock <IIngredientsService>();
            var  foodCategoriesServiceMock = new Mock <IFoodCategoriesService>();
            var  recipesServiceMock        = new Mock <IRecipesService>();
            var  mappingServiceMock        = new Mock <IMappingService>();
            var  controller = new RecipesController(recipesServiceMock.Object, ingredientsServiceMock.Object, foodCategoriesServiceMock.Object, mappingServiceMock.Object);
            Guid id         = Guid.NewGuid();
            var  recipe     = new Recipe()
            {
                Id       = id,
                Title    = "Tomato Salad",
                DishType = DishType.Salad
            };
            var model = new RecipeViewModel()
            {
                Title       = recipe.Title,
                DishType    = recipe.DishType,
                Instruction = recipe.Instruction,
                Describtion = recipe.Describtion
            };

            mappingServiceMock.Setup(x => x.Map <RecipeViewModel>(recipe)).Returns(model);

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

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

            //Act & Assert
            controller.WithCallTo(x => x.EditRecipe(model))
            .ShouldRenderView("EditRecipe")
            .WithModel <RecipeViewModel>()
            .AndModelError("Instruction");
        }
예제 #15
0
        public void RenderTheRightPartialViewWithTheCorrectModel_SearchRecipeViewModelAndNoModelErrorsAndCorrectContent()
        {
            //Arrange
            var        inredientsServiceMock     = new Mock <IIngredientsService>();
            var        foodCategoriesServiceMock = new Mock <IFoodCategoriesService>();
            var        recipesServiceMock        = new Mock <IRecipesService>();
            var        mappingServiceMock        = new Mock <IMappingService>();
            var        controller   = new RecipesController(recipesServiceMock.Object, inredientsServiceMock.Object, foodCategoriesServiceMock.Object, mappingServiceMock.Object);
            Guid       ingredientId = Guid.NewGuid();
            Ingredient ingredient   = new Ingredient()
            {
                Id = ingredientId, Name = "IngredientName", PricePerMeasuringUnit = 12.60m, QuantityInMeasuringUnit = 0
            };
            List <Ingredient> ingredients = new List <Ingredient>()
            {
                ingredient
            };

            inredientsServiceMock.Setup(x => x.GetAllIngredients()).Returns(ingredients);

            List <string> ingredientNames = new List <string>()
            {
                "Tomato"
            };
            List <double> ingredientQuantities = new List <double>()
            {
                1.200
            };
            List <decimal> ingredientPrices = new List <decimal>()
            {
                4.80m
            };
            List <Guid> foodCategories = new List <Guid>()
            {
                Guid.NewGuid()
            };

            Guid   recipeId       = Guid.NewGuid();
            Guid   foodCategoryId = Guid.NewGuid();
            Recipe recipe         = new Recipe()
            {
                Id          = recipeId,
                Title       = "Title Of A New Recipe",
                Describtion = "This is a decsribtion",
                Instruction = "Instructions of the recipe",
                DishType    = DishType.MainDish,
                Ingredients = ingredients
            };

            var recipes = new List <RecipeViewModel>()
            {
                new RecipeViewModel()
                {
                    Title       = recipe.Title,
                    Describtion = recipe.Describtion,
                    Ingredients = new List <IngredientViewModel>()
                    {
                        new IngredientViewModel()
                        {
                            Name           = ingredient.Name,
                            RecipeId       = recipeId,
                            FoodCategoryId = foodCategoryId
                        }
                    },
                    DishType    = recipe.DishType,
                    Instruction = recipe.Instruction
                }
            };


            var searchModel = new SearchRecipeViewModel()
            {
                PageSize     = 5,
                TotalRecords = recipes.Count,
                Recipes      = recipes
            };

            string title = "Title Of A New Recipe";

            //Act & Assert
            controller.WithCallTo(x => x.Search(title))
            .ShouldRenderPartialView("_RecipesGridPartial")
            .WithModel <SearchRecipeViewModel>(
                viewModel => Assert.AreEqual(recipes.ToList()[0].Title, searchModel.Recipes.ToList()[0].Title))
            .AndNoModelErrors();
        }