Example #1
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (ModelState.IsValid)
            {
                await ingredientController.CreateIngredientAsync(Ingredient);

                return(RedirectToPage("Index"));
            }

            return(Page());
        }
        public async Task <IActionResult> OnPostCreateAsync()
        {
            if (ModelState.IsValid)
            {
                try
                {
                    await _ingredientController.CreateIngredientAsync(Created);

                    return(RedirectToPage("Index"));
                }catch (Exception)
                {
                    return(RedirectToPage("/Error"));
                }
            }
            return(Page());
        }
        public async Task CreateIngredientAsync_Throws_Exception_If_Name_Null_Or_Empty()
        {
            //Arrange
            Ingredient toCreate = new Ingredient {
                Name = ""
            };
            //Act
            var caughtException = await Assert.ThrowsAsync <EmptyFieldException>(async() => await _ingredientController.CreateIngredientAsync(toCreate));

            //Assert
            _loggerMock.Verify(logger => logger.Log(
                                   It.IsAny <LogLevel>(),
                                   It.IsAny <EventId>(),
                                   It.Is <It.IsAnyType>((v, t) => true),
                                   It.IsAny <Exception>(),
                                   It.Is <Func <It.IsAnyType, Exception, string> >((v, t) => true)), Times.Once);
            Assert.Contains("name is null or empty", caughtException.Message);
        }