public async Task Create_ValidInput_ReturnCreatedResource()
    {
        var validInput = new IngredientCreateViewModel
        {
            Name = "Apple"
        };

        var result = await ingredientService.Create(validInput);

        Assert.That(result, Is.Not.Null);
        Assert.That(result.Name, Is.EqualTo(validInput.Name));
    }
        public async Task ThrowArgumentException_When_IngredientAlreadyExists()
        {
            var testIngredientName = "TestIngredientName";

            var options = TestUtilities.GetOptions(nameof(ThrowArgumentException_When_IngredientAlreadyExists));

            var ingredient = new Ingredient()
            {
                Name = testIngredientName
            };

            using (var arrangeContext = new AppDBContext(options))
            {
                await arrangeContext.Ingredients.AddAsync(new IngredientEntity()
                {
                    Name = testIngredientName
                });

                await arrangeContext.SaveChangesAsync();
            }

            using (var assertContext = new AppDBContext(options))
            {
                var sut = new IngredientService(assertContext);
                await Assert.ThrowsExceptionAsync <ArgumentException>(() => sut.Create(ingredient));
            }
        }
 public IActionResult Create([Bind("IngredientId,Name,Price")] Ingredient ingredient)
 {
     if (ModelState.IsValid)
     {
         _ingredientService.Create(ingredient);
         return(RedirectToAction(nameof(Index)));
     }
     return(View(ingredient));
 }
示例#4
0
 public async Task <IActionResult> Create([FromBody] IngredientCreateViewModel ingredientCreateViewModel)
 {
     try
     {
         return(Ok(await ingredientService.Create(ingredientCreateViewModel)));
     }
     catch (ResourceAlreadyExistsException exception)
     {
         logger.Error("{Message}", exception.Message);
         return(BadRequest(exception.Message));
     }
     catch (Exception exception)
     {
         logger.Error("{Message}", exception.Message);
         return(Problem());
     }
 }
        public async Task CreateIngredient_When_SuchIngredientDoesNotExist()
        {
            var testIngredientName = "TestIngredientName";

            var options = TestUtilities.GetOptions(nameof(CreateIngredient_When_SuchIngredientDoesNotExist));

            using (var arrangeContext = new AppDBContext(options))
            {
                await arrangeContext.Ingredients.AddAsync(new IngredientEntity()
                {
                    Name = "RandomIngredientName"
                });

                await arrangeContext.SaveChangesAsync();
            }

            using (var actContext = new AppDBContext(options))
            {
                var sut        = new IngredientService(actContext);
                var ingredient = new Ingredient()
                {
                    Name = testIngredientName
                };
                var testIngredient = await sut.Create(ingredient);

                await actContext.SaveChangesAsync();
            }

            using (var assertContext = new AppDBContext(options))
            {
                Assert.AreEqual(2, assertContext.Ingredients.Count());
                var cocktail = await assertContext.Ingredients.FirstOrDefaultAsync(x => x.Name == testIngredientName);

                Assert.IsNotNull(cocktail);
            }
        }