예제 #1
0
        public async Task AddIngredientToRecipeAsync_Throws_EntryNotFoundException_On_NonExistent_Recipe()
        {
            //Arrange
            List <Recipe> mockRecipeDB = GetRecipes();
            Recipe        recipeToTest = new Recipe {
                Id = 0, Name = "Recipe 1", CategoryId = 1
            };                                                                              // Non-existent in DB recipe
            string ingredientName = "beef";
            double amount         = 800;
            string measure        = "g";

            _mockRepository.Setup(r => r.FirstOrDefaultAsync <Recipe>(It.IsAny <Expression <Func <Recipe, bool> > >()))
            .ReturnsAsync(() => mockRecipeDB.FirstOrDefault(x => string.Equals(x.Name, recipeToTest.Name, StringComparison.OrdinalIgnoreCase) && x.CategoryId == recipeToTest.CategoryId && x.Id == recipeToTest.Id));
            //Act
            var caughtException = await Assert.ThrowsAsync <EntryNotFoundException>(async() => await _recipeController.AddIngredientToRecipeAsync(recipeToTest, ingredientName, measure, amount));

            //Assert
            _mockRepository.Verify(r => r.FirstOrDefaultAsync <Recipe>(It.IsAny <Expression <Func <Recipe, bool> > >()), Times.Once);
            Assert.Contains("doesn't exist in Database", caughtException.Message);
        }
예제 #2
0
        private static async Task FormIngredientListAsync(Recipe recipeToAdd, RecipeController recCont)
        {
            Console.WriteLine($"Enter ingredients for recipe {recipeToAdd.Name} below:");
            Console.WriteLine("Or enter -1 in any field to stop adding ingredients");

            while (true)
            {
                Console.Write("Ingredient name:");
                string name = Console.ReadLine().Trim();
                if (name == "-1")
                {
                    break;
                }

                double amount;
                bool   wasBreaked = false;
                bool   parsed     = false;
                while (true)
                {
                    Console.Write("Amount:");
                    parsed = double.TryParse(Console.ReadLine().Trim(), out amount);
                    if (parsed && amount == -1)
                    {
                        wasBreaked = true;
                        break;
                    }
                    else if (parsed)
                    {
                        break;
                    }
                }
                if (wasBreaked)
                {
                    break;
                }
                Console.Write("Measured in:");
                string measure = Console.ReadLine().Trim();
                if (measure == "-1")
                {
                    break;
                }
                try
                {
                    await recCont.AddIngredientToRecipeAsync(recipeToAdd, name, measure, amount);
                }
                catch (Exception outer)
                {
                    _logger.LogError(outer.Message);
                    Console.WriteLine("Ingredient wasn't added to recipe, please try again");
                }
            }
        }
        public async Task <IActionResult> OnPostAddIngredientAsync(string newIngredient, double newAmount, int measureId)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    await _recipeController.AddIngredientToRecipeAsync(RecipeToEdit, newIngredient, newAmount, measureId);
                }
                catch (Exception)
                {
                    return(RedirectToPage("/Error"));
                }
            }
            else
            {
                return(RedirectToPage("/Error"));
            }

            return(RedirectToPage("/Recipes/Edit", new { Id = RecipeToEdit.Id }));
        }