Ejemplo n.º 1
0
        public void AddIngredient_NewRecordAddedWithProperValues()
        {
            //Arrange
            var dbOptions = new DbContextOptionsBuilder <CarbonKitchenDbContext>()
                            .UseInMemoryDatabase(databaseName: $"IngredientDb{Guid.NewGuid()}")
                            .Options;
            var sieveOptions = Options.Create(new SieveOptions());

            var fakeIngredient = new FakeIngredient {
            }.Generate();

            //Act
            using (var context = new CarbonKitchenDbContext(dbOptions))
            {
                var service = new IngredientRepository(context, new SieveProcessor(sieveOptions));

                service.AddIngredient(fakeIngredient);

                context.SaveChanges();
            }

            //Assert
            using (var context = new CarbonKitchenDbContext(dbOptions))
            {
                context.Ingredients.Count().Should().Be(1);

                var ingredientById = context.Ingredients.FirstOrDefault(i => i.IngredientId == fakeIngredient.IngredientId);

                ingredientById.Should().BeEquivalentTo(fakeIngredient);
                ingredientById.IngredientId.Should().Be(fakeIngredient.IngredientId);
                ingredientById.Name.Should().Be(fakeIngredient.Name);
                ingredientById.Unit.Should().Be(fakeIngredient.Unit);
                ingredientById.Amount.Should().Be(fakeIngredient.Amount);
            }
        }
Ejemplo n.º 2
0
 public ActionResult AddIngredient(Guid id, Ingredient ing)
 {
     try
     {
         if (ModelState.IsValid)
         {
             ing.recipeId = id;
             if (_repo.AddIngredient(ing))
             {
                 ViewBag.Message = "Ingredient added successfully";
                 return(RedirectToAction("GetIngredients", "Recipe", new { id = ing.recipeId }));
             }
         }
         return(View());
     }
     catch
     {
         return(View());
     }
 }