public void RedirectToActionIndex__WhenIngredientIsSuccessfullyDeleted() { //Arrange var ingredientsServiceMock = new Mock <IIngredientsService>(); var foodCategoriesServiceMock = new Mock <IFoodCategoriesService>(); var recipesServiceMock = new Mock <IRecipesService>(); var mappingServiceMock = new Mock <IMappingService>(); var controller = new IngredientsController(ingredientsServiceMock.Object, foodCategoriesServiceMock.Object, recipesServiceMock.Object, mappingServiceMock.Object); var id = Guid.NewGuid(); Ingredient ingredient = new Ingredient(); ingredient.Id = id; ingredient.Name = "Carrot"; ingredient.FoodCategoryId = Guid.NewGuid(); ingredient.PricePerMeasuringUnit = 1.80m; ingredient.QuantityInMeasuringUnit = 2; ingredientsServiceMock.Setup(x => x.GetIngredientById(id)).Returns(ingredient); ingredientsServiceMock.Setup(x => x.DeleteIngredient(ingredient)); //Act & Assert controller.WithCallTo(x => x.DeleteIngredientConfirm(id)) .ShouldRedirectTo(x => x.Index()); }
public void RenderTheRightViewWithTheCorrectModel_SearchIngredientViewModelAndNoModelErrorsAndCorrectContent() { //Arrange Guid ingredientId = Guid.NewGuid(); Ingredient ingredient = new Ingredient() { Id = ingredientId, Name = "IngredientName", PricePerMeasuringUnit = 12.60m, QuantityInMeasuringUnit = 0 }; IEnumerable <Ingredient> ingredients = new List <Ingredient>() { 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 searchModel = new SearchIngredientViewModel(); searchModel.Ingredients = ingredients.Select(x => new IngredientViewModel() { Name = x.Name }); var controller = new IngredientsController(inredientsServiceMock.Object, foodCategoriesServiceMock.Object, recipesServiceMock.Object, mappingServiceMock.Object); controller.Index(); //Act & Assert controller.WithCallTo(x => x.Index()) .ShouldRenderView("Index") .WithModel <SearchIngredientViewModel>( viewModel => Assert.AreEqual(ingredients.ToList()[0].Name, searchModel.Ingredients.ToList()[0].Name)) .AndNoModelErrors(); }
public void RenderTheRightView_AddIngredient_WithTheCorrectModel_IngredientViewModel_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 IngredientsController(ingredientsServiceMock.Object, foodCategoriesServiceMock.Object, recipesServiceMock.Object, mappingServiceMock.Object); AddIngredientViewModel ingredientModel = new AddIngredientViewModel(); ingredientModel.Name = null; var selectedFoodCategoryId = Guid.NewGuid(); ingredientModel.SelectedFoodCategoryId = selectedFoodCategoryId; ingredientModel.PricePerMeasuringUnit = 1.80m; ingredientModel.QuantityInMeasuringUnit = 2; var validationContext = new ValidationContext(ingredientModel, null, null); var validationResults = new List <ValidationResult>(); Validator.TryValidateObject(ingredientModel, validationContext, validationResults, true); foreach (var validationResult in validationResults) { controller.ModelState.AddModelError(validationResult.MemberNames.First(), validationResult.ErrorMessage); } //Act & Assert controller.WithCallTo(x => x.AddIngredient(ingredientModel)) .ShouldRenderView("AddIngredient") .WithModel <AddIngredientViewModel>() .AndModelError("Name"); }
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 IngredientsController(ingredientsServiceMock.Object, foodCategoriesServiceMock.Object, recipesServiceMock.Object, mappingServiceMock.Object); IngredientViewModel ingredientModel = new IngredientViewModel(); ingredientModel.Name = "Carrot"; ingredientModel.FoodCategory = new FoodCategory() { Name = "Tomatos", Id = Guid.NewGuid(), MeasuringUnit = MeasuringUnitType.Kg, FoodType = FoodType.Vegetable }; ingredientModel.PricePerMeasuringUnit = 1.80m; ingredientModel.QuantityInMeasuringUnit = 2; //Act & Assert controller.WithCallTo(x => x.EditIngredient(ingredientModel)) .ShouldRedirectTo(x => x.Index()); }
public void RenderTheRightView_AddIngredient() { //Arrange var inredientsServiceMock = new Mock <IIngredientsService>(); var foodCategoriesServiceMock = new Mock <IFoodCategoriesService>(); var recipesServiceMock = new Mock <IRecipesService>(); var mappingServiceMock = new Mock <IMappingService>(); var controller = new IngredientsController(inredientsServiceMock.Object, foodCategoriesServiceMock.Object, recipesServiceMock.Object, mappingServiceMock.Object); //Act & Assert controller.WithCallTo(x => x.AddIngredient()).ShouldRenderView("AddIngredient"); }
public void RedirectToErrorPage_When_IdGuidIsNotValid() { //Arrange var inredientsServiceMock = new Mock <IIngredientsService>(); var foodCategoriesServiceMock = new Mock <IFoodCategoriesService>(); var recipesServiceMock = new Mock <IRecipesService>(); var mappingServiceMock = new Mock <IMappingService>(); var controller = new IngredientsController(inredientsServiceMock.Object, foodCategoriesServiceMock.Object, recipesServiceMock.Object, mappingServiceMock.Object); //Act & Assert controller.WithCallTo(x => x.DeleteIngredient(Guid.Empty)) .ShouldRenderView("404.html"); }
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 IngredientsController(inredientsServiceMock.Object, foodCategoriesServiceMock.Object, recipesServiceMock.Object, mappingServiceMock.Object); //Act & Assert controller.WithCallTo(x => x.Index()).ShouldRenderView("Index"); }
public void RenderTheRightView_DeleteIngredient__WhenIngredientWasNotFoundInDatabase() { //Arrange var ingredientsServiceMock = new Mock <IIngredientsService>(); var foodCategoriesServiceMock = new Mock <IFoodCategoriesService>(); var recipesServiceMock = new Mock <IRecipesService>(); var mappingServiceMock = new Mock <IMappingService>(); var controller = new IngredientsController(ingredientsServiceMock.Object, foodCategoriesServiceMock.Object, recipesServiceMock.Object, mappingServiceMock.Object); var id = Guid.NewGuid(); ingredientsServiceMock.Setup(x => x.GetIngredientById(id)).Returns <Ingredient>(null); //Act & Assert controller.WithCallTo(x => x.DeleteIngredient(id)) .ShouldRenderView("DeleteIngredient"); }
public void RenderTheRightPartialViewWithTheCorrectModel_SearchIngredientViewModelAndNoModelErrors() { //Arrange IEnumerable <Ingredient> ingredients = new List <Ingredient>(); string name = "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 IngredientsController(inredientsServiceMock.Object, foodCategoriesServiceMock.Object, recipesServiceMock.Object, mappingServiceMock.Object); //Act & Assert controller.WithCallTo(x => x.Search(name)) .ShouldRenderPartialView("_IngredientsGridPartial") .WithModel <SearchIngredientViewModel>() .AndNoModelErrors(); }
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 IngredientsController(ingredientsServiceMock.Object, foodCategoriesServiceMock.Object, recipesServiceMock.Object, mappingServiceMock.Object); AddIngredientViewModel ingredientModel = new AddIngredientViewModel(); ingredientModel.Name = "Pink Tomato"; var selectedFoodCategoryId = Guid.NewGuid(); ingredientModel.SelectedFoodCategoryId = selectedFoodCategoryId; ingredientModel.PricePerMeasuringUnit = 1.80m; ingredientModel.QuantityInMeasuringUnit = 2; //Act & Assert controller.WithCallTo(x => x.AddIngredient(ingredientModel)) .ShouldRedirectTo(x => x.Index()); }
public void RenderTheRightView_EditIngredient_WhenValidGuidIdIsPassed() { //Arrange var foodCategoriesModels = new List <FoodCategoryViewModel>() { new FoodCategoryViewModel() { Id = Guid.NewGuid(), Name = "Tomatos", FoodType = FoodType.Vegetable, MeasuringUnit = MeasuringUnitType.Kg } }; var recipesModels = new List <RecipeViewModel>() { new RecipeViewModel() { Id = Guid.NewGuid(), Title = "Tomato Salad", DishType = DishType.Salad, Describtion = "Some describtion", Instruction = "These are the instructions" } }; var foodCategories = new List <FoodCategory>() { new FoodCategory() { Id = Guid.NewGuid(), Name = "Tomatos", FoodType = FoodType.Vegetable, MeasuringUnit = MeasuringUnitType.Kg } }; var recipes = new List <Recipe>() { new Recipe() { Id = Guid.NewGuid(), Title = "Tomato Salad", DishType = DishType.Salad, Describtion = "Some describtion", Instruction = "These are the instructions" } }; var ingredientsServiceMock = new Mock <IIngredientsService>(); var foodCategoriesServiceMock = new Mock <IFoodCategoriesService>(); foodCategoriesServiceMock.Setup(x => x.GetAllFoodCategories()).Returns(foodCategories); var recipesServiceMock = new Mock <IRecipesService>(); recipesServiceMock.Setup(x => x.GetAllRecipes()).Returns(recipes); var mappingServiceMock = new Mock <IMappingService>(); var controller = new IngredientsController(ingredientsServiceMock.Object, foodCategoriesServiceMock.Object, recipesServiceMock.Object, mappingServiceMock.Object); var id = Guid.NewGuid(); var name = "Tomato"; var foodcategoryId = Guid.NewGuid(); var recipeId = Guid.NewGuid(); var ingredient = new Ingredient() { Id = id, Name = name, FoodCategoryId = foodcategoryId, RecipeId = recipeId }; ingredientsServiceMock.Setup(x => x.GetIngredientById(id)).Returns(ingredient); var model = new IngredientViewModel(); model.FoodCategoryId = ingredient.FoodCategoryId; model.Name = ingredient.Name; model.RecipeId = ingredient.RecipeId; model.Id = ingredient.Id; model.FoodCategories = foodCategoriesModels; model.Recipes = recipesModels; mappingServiceMock.Setup(x => x.Map <IngredientViewModel>(ingredient)).Returns(model); //Act & Assert controller.WithCallTo(x => x.EditIngredient(id)) .ShouldRenderView("EditIngredient") .WithModel(model); }