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 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 async Task CreateRecipe_Returns_Ok_When_Arguments_Are_Valid()
        {
            // Arrange
            var recipesService = Substitute.For <IRecipesService>();

            recipesService
            .CreateAsync(Arg.Any <Guid>(), Arg.Any <string>(), Arg.Any <bool>(), Arg.Any <string>(), Arg.Any <string>(), Arg.Any <TimeSpan?>(), Arg.Any <int?>(), Arg.Any <string>(), Arg.Any <IList <Ingredient> >(), Arg.Any <IList <Step> >(), Arg.Any <IList <Tag> >())
            .Returns(Task.CompletedTask);
            var controller = new RecipesController(recipesService, AutoMapper.Mapper.Instance);
            var recipe     = new NewRecipeDTO {
                Title       = "Naleśniki",
                Description = "Naleśniki z serem",
                Image       = null,
                Duration    = TimeSpan.FromMinutes(30),
                Servings    = 12,
                Notes       = "Idealne na lekki głód",
                Ingredients = new List <NewRecipeIngredientDTO>(),
                Steps       = new List <NewRecipeStepDTO>(),
                Tags        = new List <NewRecipeTagDTO>()
            };

            // Act
            var result = await controller.CreateRecipe(recipe);

            // Assert
            Assert.True(result.GetType().IsAssignableFrom(typeof(OkResult)));
        }
        public async Task TestUpdatesRecipeWithoutIngredients()
        {
            DatabaseFixture fixture             = new DatabaseFixture();
            var             options             = fixture.options;
            var             recipesContext      = new RecipesContext(options);
            var             emptyIngredientList = new List <Ingredient>();
            var             existingIngredient  = new Recipe
            {
                Description = "existing description", Name = "existing title", Ingredients = emptyIngredientList, Id = 9
            };
            await recipesContext.Recipes.AddAsync(existingIngredient);

            await recipesContext.SaveChangesAsync();

            var controller = new RecipesController(recipesContext);

            var emptyList = Array.Empty <IngredientDto>();
            var newRecipe = new RecipeDto("Updated recipe", "Updated description", emptyList)
            {
                Id = 9
            };
            await controller.PatchRecipe(9, newRecipe);

            var updatedRecipe = await recipesContext.Recipes.FindAsync((long)9);

            Assert.Equal("Updated recipe", updatedRecipe.Name);
            Assert.Equal("Updated description", updatedRecipe.Description);
            Assert.Empty(updatedRecipe.Ingredients);
        }
        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 GetAllRecipe()
        {
            var repository = new Mock <IRepository <Recipe> >();
            var recipes    = new List <Recipe>();
            var r1         = new Recipe()
            {
                recipeId = 1, userId = 1, categoryId = 2, calories = 500, preparation = "zazaaza"
            };
            var r2 = new Recipe()
            {
                recipeId = 2, userId = 3, categoryId = 2, calories = 500, preparation = "zazaaza"
            };
            var r3 = new Recipe()
            {
                recipeId = 3, userId = 2, categoryId = 2, calories = 500, preparation = "zazaaza"
            };

            recipes.Add(r1);
            recipes.Add(r2);
            recipes.Add(r3);
            repository.Setup(x => x.GetAll()).Returns(recipes.AsQueryable()).Verifiable();
            var controller = new RecipesController(repository.Object);

            controller.Request       = new HttpRequestMessage();
            controller.Configuration = new HttpConfiguration();

            var response = controller.Get();
            IQueryable <Recipe> s;

            Assert.IsTrue(response.TryGetContentValue <IQueryable <Recipe> >(out s));
            Assert.AreEqual(recipes.AsQueryable().Count(), s.Count());
            Assert.AreEqual(recipes.AsQueryable().First(), s.First());
        }
Exemple #7
0
        public async Task GetRecipe_Returns_RecipeWithSameContent()
        {
            // Arrange
            var mockRepository = new Mock <IRecipesRepository>();

            mockRepository.Setup(x => x.GetRecipe(1))
            .ReturnsAsync(_testData.RecipeData);

            var controller = new RecipesController(mockRepository.Object);

            // Act
            Task <IHttpActionResult> actionResult = controller.GetRecipe(1);
            var contentResult = await actionResult as OkNegotiatedContentResult <Recipe>;

            // Assert
            Assert.IsNotNull(contentResult);
            Assert.IsNotNull(contentResult.Content);
            Assert.AreEqual(_testData.RecipeData.Id, contentResult.Content.Id);
            Assert.AreEqual(_testData.RecipeData.Name, contentResult.Content.Name);
            Assert.AreEqual(_testData.RecipeData.Description, contentResult.Content.Description);
            Assert.AreEqual(_testData.RecipeData.Ingredients, contentResult.Content.Ingredients);
            Assert.AreEqual(_testData.RecipeData.Directions, contentResult.Content.Directions);
            Assert.AreEqual(_testData.RecipeData.Type, contentResult.Content.Type);
            Assert.AreEqual(_testData.RecipeData.ImageLink, contentResult.Content.ImageLink);
            Assert.AreEqual(_testData.RecipeData.DateCreated, contentResult.Content.DateCreated);
            Assert.AreEqual(_testData.RecipeData.Notes, contentResult.Content.Notes);
        }
        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();
        }
Exemple #9
0
        public async void CanGetOneRecipeNotFound()
        {
            DbContextOptions <CookBookDbContext> options = new DbContextOptionsBuilder <CookBookDbContext>().UseInMemoryDatabase("CanGettRecipeOk").Options;

            using (CookBookDbContext context = new CookBookDbContext(options))
            {
                //Arrange
                Recipes recipe = new Recipes();
                recipe.ID   = 1;
                recipe.Name = "Sketti n Ketchup";
                Recipes recipe2 = new Recipes();
                recipe2.ID   = 2;
                recipe2.Name = "Bread n Water";
                Recipes recipe3 = new Recipes();
                recipe3.ID   = 3;
                recipe3.Name = "Knuckle Sandwich";

                //Act
                RecipesController recipesController = new RecipesController(context, configuration);
                await recipesController.Post(recipe);

                await recipesController.Post(recipe2);

                var data = recipesController.Get(6);

                //Assert
                Assert.IsType <NotFoundResult>(data);
            }
        }
Exemple #10
0
        public void Edit()
        {
            var mock = new Mock <IDbContext>();

            mock.Setup(x => x.Set <Recipe>())
            .Returns(new FakeDbSet <Recipe>
            {
                new Recipe {
                    RecipeId    = 1,
                    Name        = "Zupa pomidorowa",
                    Description = "asfagaghahahadh",
                    Difficulty  = DifficultyEnum.easy,
                    PublishDate = DateTime.Now
                }
            });

            var obj    = mock.Object;
            var newObj = new Recipe
            {
                RecipeId    = 1,
                Name        = "Zupa ogórkowa",
                Description = "asfagaghahahadh",
                Difficulty  = DifficultyEnum.easy,
                PublishDate = DateTime.Now
            };

            var controller = new RecipesController(mock.Object);
            var result     = controller.Edit(newObj);
            var editedObj  = obj.Set <Recipe>().FirstOrDefault(p => p.RecipeId == newObj.RecipeId);

            Assert.AreEqual(newObj.Name, editedObj.Name);
        }
Exemple #11
0
        public void TestMethod1()
        {
            var controller = new RecipesController();
            var result     = controller.Landing() as ViewResult;

            Assert.AreEqual("LandingRecipes", result.ViewName);
        }
Exemple #12
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));
            });
        }
Exemple #13
0
        public void TestGenerationCombination()
        {
            var keys = generateKeys(4);

            var controller = new RecipesController();

            var combos = controller.getZobrists(keys);

            Assert.Equal(15, combos.Count());

            Assert.Contains(keys[0], combos);
            Assert.Contains(keys[1], combos);
            Assert.Contains(keys[2], combos);
            Assert.Contains(keys[3], combos);

            Assert.Contains(keys[0] ^ keys[1], combos);
            Assert.Contains(keys[0] ^ keys[2], combos);
            Assert.Contains(keys[0] ^ keys[3], combos);
            Assert.Contains(keys[1] ^ keys[2], combos);
            Assert.Contains(keys[1] ^ keys[3], combos);
            Assert.Contains(keys[2] ^ keys[3], combos);

            Assert.Contains(keys[0] ^ keys[1] ^ keys[2], combos);
            Assert.Contains(keys[0] ^ keys[1] ^ keys[3], combos);
            Assert.Contains(keys[1] ^ keys[2] ^ keys[3], combos);
            Assert.Contains(keys[0] ^ keys[2] ^ keys[3], combos);

            Assert.Contains(keys[0] ^ keys[1] ^ keys[2] ^ keys[3], combos);
        }
Exemple #14
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);
        }
 public RecipesControllerTest()
 {
     DB      = new InMemoryDB();
     context = DB.GetInMemoryDB(true);
     SeederInMemoryDB.Seed(context);
     rc = new RecipesController(context);
 }
Exemple #16
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());
        }
Exemple #17
0
        public async Task Edit_Null_Id()
        {
            var controller = new RecipesController(_service.Object);

            var result = await controller.Edit(null, _recipe);

            Assert.IsType <BadRequestResult>(result);
        }
        public void GetRecipeByID_Returns404NotFound_WhenNonExistentIDProvided()
        {
            mockRepo.Setup(repo => repo.GetRecipeById(0)).Returns(() => null);
            var controller = new RecipesController(mockRepo.Object, mapper);
            var result     = controller.GetRecipeById(1);

            Assert.IsType <NotFoundResult>(result.Result);
        }
Exemple #19
0
        public void CreateRecipe_ReturnsCorrectResourceType_WhenValidObjectSubmitted()
        {
            var controller = new RecipesController(mockRepo.Object, mapper);
            var result     = controller.CreateRecipe(new RecipeCreateDto {
            });

            Assert.IsInstanceOf <ActionResult <RecipeReadDto> >(result);
        }
Exemple #20
0
        public async Task Edit()
        {
            var controller = new RecipesController(_service.Object);

            var result = await controller.Edit(_recipe.Id, _recipe);

            Assert.IsType <OkResult>(result);
        }
Exemple #21
0
        public async Task Details_Bad_Id()
        {
            var controller = new RecipesController(_service.Object);

            var result = await controller.Details(Guid.NewGuid());

            Assert.IsType <NotFoundResult>(result);
        }
Exemple #22
0
        public async Task Delete()
        {
            var controller = new RecipesController(_service.Object);

            var result = await controller.Delete(_recipe.Id);

            Assert.IsType <OkResult>(result);
        }
        public void GetAllRecipes_ReturnsZeroItems_WhenDBIsEmpty()
        {
            mockRepo.Setup(repo =>
                           repo.GetAllRecipes()).Returns(GetRecipes(0));
            var controller = new RecipesController(mockRepo.Object, mapper);
            var result     = controller.GetAllRecipes();

            Assert.IsType <OkObjectResult>(result.Result);
        }
        public void DeleteRecipe_Returns404NotFound_WhenNonExistentResourceIDSubmitted()
        {
            mockRepo.Setup(repo =>
                           repo.GetRecipeById(0)).Returns(() => null);
            var controller = new RecipesController(mockRepo.Object, mapper);
            var result     = controller.DeleteRecipe(0);

            Assert.IsType <NotFoundResult>(result);
        }
Exemple #25
0
        public async Task RecipeInstructions_OkResult()
        {
            RecipeRepository recipe = new RecipeRepository(_options, new HttpClient(), _cacheServiceMock.Object);

            var controller = new RecipesController(recipe, _mapper, _logger);
            var result     = await controller.RecipeInstructions(1, false);

            result.Should().NotBeNull();
            var okResult = result.Should().BeOfType <OkObjectResult>().Subject;
        }
        public void GetAllRecipes_ReturnsOneItem_WhenDBHasOneResource()
        {
            mockRepo.Setup(repo => repo.GetAllRecipes()).Returns(GetRecipes(1));
            var controller = new RecipesController(mockRepo.Object, mapper);
            var result     = controller.GetAllRecipes();
            var okResult   = result.Result as OkObjectResult;
            var recipes    = okResult.Value as List <RecipeReadDto>;

            Assert.Single(recipes);
        }