Ejemplo n.º 1
0
        public async Task CreateRecipe_Should_Be_Created_New_Recipe()
        {
            // Arrange
            var recipeToAdd = new Recipe {
                Name = "Recipe 1"
            };

            var repositoryMock = new Mock <IRecipeRepository>();

            repositoryMock.Setup(r => r.SingleOrDefaultAsync(x => x.Name == recipeToAdd.Name)).ReturnsAsync((Recipe)null);
            repositoryMock.Setup(r => r.Add(recipeToAdd));

            _unitOfWorkMock.SetupGet(uow => uow.Recipes).Returns(repositoryMock.Object);
            _unitOfWorkMock.Setup(uow => uow.SaveChangesAsync());

            // Act
            var result = await _controller.CreateRecipeAsync(recipeToAdd);

            // Assert
            Assert.Equal(result, recipeToAdd);

            repositoryMock.Verify(r => r.SingleOrDefaultAsync(x => x.Name == recipeToAdd.Name), Times.Once);
            repositoryMock.Verify(r => r.Add(recipeToAdd), Times.Once);
            _unitOfWorkMock.VerifyGet(uow => uow.Recipes, Times.AtLeastOnce);
            _unitOfWorkMock.Verify(uow => uow.SaveChangesAsync(), Times.Once);
        }
Ejemplo n.º 2
0
        public async Task CreateRecipe_IfExists_ReturnRecipe()
        {
            //Arrange
            MakeMockGetWithIncludeEntityForRepository();

            // Act
            // Run method which should be tested
            await _controller.CreateRecipeAsync("expected", 1, "expected");

            // Assert
            // Check if the entity has been return from method CreateRecipeAsync
            _repositoryMock.VerifyAll();
            Assert.NotNull(_controller.CurrentRecipe);
            Assert.Same(_expectedRecipe, _controller.CurrentRecipe);
        }
Ejemplo n.º 3
0
        public async Task CreateRecipeAsync_Success(NewRecipe newRecipe, FullRecipe recipe, int recipeId)
        {
            A.CallTo(() => recipeService.CreateRecipeAsync(newRecipe)).Returns(recipeId);
            A.CallTo(() => recipeService.GetRecipeAsync(recipeId)).Returns(recipe);

            var result = await sut.CreateRecipeAsync(newRecipe).ConfigureAwait(false);

            A.CallTo(() => recipeService.CreateRecipeAsync(newRecipe)).MustHaveHappenedOnceExactly();
            result.Should().Be(recipe);
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (ModelState.IsValid)
            {
                await recipeController.CreateRecipeAsync(Recipe);

                return(RedirectToPage("Index"));
            }

            return(Page());
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> OnPostCreateAsync()
        {
            if (ModelState.IsValid)
            {
                try
                {
                    await _recipeController.CreateRecipeAsync(Created);

                    return(RedirectToPage("/Recipes/Index"));
                }catch (Exception)
                {
                    return(RedirectToPage("/Error"));
                }
            }
            return(Page());
        }
Ejemplo n.º 6
0
        public async Task CreateRecipe_Should_Throw_When_RecipeNameEmpty()
        {
            // Arrange
            var recipeToAdd  = new Recipe {
            };
            Recipe newRecipe = null;

            _unitOfWorkMock.Setup(uow => uow.SaveChangesAsync());
            // Act & Assert
            var controller = new RecipeController(_unitOfWorkMock.Object);
            var exception  = await Assert.ThrowsAsync <EmptyFieldException>(async() => newRecipe = await controller.CreateRecipeAsync(recipeToAdd));

            Assert.EndsWith("cannot be empty.", exception.Message);
            Assert.Null(newRecipe);
            _unitOfWorkMock.Verify(uow => uow.SaveChangesAsync(), Times.Never);
        }
Ejemplo n.º 7
0
        public static async Task AddRecipeAsync(CategoryController categoryController, RecipeController recipeController, IngredientController ingredientController, string answer = "", int count = 0)
        {
            Console.WriteLine("Введите название рецепта: ");
            var name = Console.ReadLine();

            DisplayCategoryTree(await categoryController.GetCategoriesAsync(), null, false);
            Console.Write("Ввидите категорию блюда (id) : ");
            await categoryController.WalkCategoriesAsync(Console.ReadLine());

            var categoryNew = categoryController.CurrentCategory;

            if (categoryNew == null)
            {
                throw new ArgumentException(nameof(categoryNew), "Null categoryNew in new Recipe");
            }

            Console.Clear();

            Console.WriteLine("Введите описание блюда: ");
            var description = Console.ReadLine();

            Console.Clear();

            do
            {
                Console.WriteLine("Введите колличество ингредиентов: ");
                answer = Console.ReadLine();
            } while (!int.TryParse(answer, out count));

            var ingredientsId = new List <int>();

            for (int i = 1; i <= count; i++)
            {
                Console.WriteLine("Введите ингредиент:");
                Console.Write($"{i}. ");
                ingredientsId.Add(await ingredientController.AddedIfNewAsync(Console.ReadLine()));
            }

            Console.WriteLine("\t\t*enter*");
            Console.ReadKey();
            Console.Clear();

            List <string> countIngred     = new List <string>();
            var           tempIngredients = await ingredientController.GetIngredientsAsync();

            for (int id = 0; id < ingredientsId.Count; id++)
            {
                Console.WriteLine("Введите колличество для " +
                                  $"\"{tempIngredients.First(i => i.Id == ingredientsId[id]).Name}\": ");
                countIngred.Add(Console.ReadLine());
            }

            int countSteps = 0;

            do
            {
                Console.WriteLine();
                Console.Clear();
                Console.Write("Введите колличество шагов приготовления: ");
                answer = Console.ReadLine();
            } while(!int.TryParse(answer, out countSteps));
            List <string> stepsHowCooking = new List <string>();

            Console.WriteLine();
            for (int i = 1; i <= countSteps; i++)
            {
                Console.WriteLine($"Введите описания шага {i} : ");
                answer = Console.ReadLine();
                stepsHowCooking.Add(answer);
            }
            await recipeController.CreateRecipeAsync(name, categoryNew.Id, description);

            await recipeController.AddedIngredientsInRecipeAsync(ingredientsId, countIngred);

            await recipeController.AddedStepsInRecipeAsync(stepsHowCooking);
        }