public void DeleteDish()
        {
            var options = new DbContextOptionsBuilder <CookDbContext>()
                          .UseInMemoryDatabase(databaseName: "CookTest")
                          .Options;

            // Use a separate instance of the context to verify correct data was saved to database
            using (var context = new CookDbContext(options))
            {
                var service = new EFDishService(context);

                service.CreateDish(dish3);
                context.SaveChanges();
            }

            // Use a separate instance of the context to verify correct data was saved to database
            using (var context = new CookDbContext(options))
            {
                var service = new EFDishService(context);

                service.DeleteDish(dish3);
                context.SaveChanges();

                var getDish = service.GetDishById(dish3.Id);

                Assert.Null(getDish);
            }
        }
        public void UpdateDish()
        {
            var options = new DbContextOptionsBuilder <CookDbContext>()
                          .UseInMemoryDatabase(databaseName: "CookTest")
                          .Options;

            // Run the test against one instance of the context
            using (var context = new CookDbContext(options))
            {
                var service = new EFDishService(context);
                service.CreateDish(dish);
                service.CreateDish(dish2);
                context.SaveChanges();
            }

            // Use a separate instance of the context to verify correct data was saved to database
            using (var context = new CookDbContext(options))
            {
                var service = new EFDishService(context);

                dish.Name        = "Testing this dish";
                dish.Price       = 7.99;
                dish.Description = "hhahaha";

                service.UpdateDish(dish);
                context.SaveChanges();

                Assert.Equal("Testing this dish", dish.Name);
                Assert.Equal(7.99, dish.Price);
                Assert.Equal("hhahaha", dish.Description);
            }
        }
        public void GetDishById()
        {
            var options = new DbContextOptionsBuilder <CookDbContext>()
                          .UseInMemoryDatabase(databaseName: "CookTest")
                          .Options;

            // Run the test against one instance of the context
            using (var context = new CookDbContext(options))
            {
                var service = new EFDishService(context);
                service.CreateDish(dish2);
                context.SaveChanges();
            }

            // Use a separate instance of the context to verify correct data was saved to database
            using (var context = new CookDbContext(options))
            {
                var service = new EFDishService(context);

                Dish dish   = new Dish();
                var  dishes = service.GetDishes();
                foreach (var item in dishes)
                {
                    if (item.Name == dish2.Name)
                    {
                        dish = item;
                    }
                }
                var getDish = service.GetDishById(dish.Id);
                Assert.Equal(dish, getDish);
            }
        }
Exemplo n.º 4
0
 public void Save()
 {
     try
     {
         _dbContext.SaveChanges();
     }
     catch (InvalidCastException e)
     {
         // Perform some action here, and then throw a new exception.
         throw;
     }
 }
        public void CreateNewDish()
        {
            var options = new DbContextOptionsBuilder <CookDbContext>()
                          .UseInMemoryDatabase(databaseName: "CookTest")
                          .Options;

            // Run the test against one instance of the context
            using (var context = new CookDbContext(options))
            {
                var service = new EFDishService(context);
                service.CreateDish(dish);
                context.SaveChanges();
            }

            // Use a separate instance of the context to verify correct data was saved to database
            using (var context = new CookDbContext(options))
            {
                Assert.Equal("Testing this dish", context.Dish.FirstOrDefault(d => d.Id == dish.Id).Name);;
                Assert.Equal(7.99, context.Dish.FirstOrDefault(d => d.Id == dish.Id).Price);
            }
        }
        public void GetAllDishes()
        {
            var options = new DbContextOptionsBuilder <CookDbContext>()
                          .UseInMemoryDatabase(databaseName: "CookTest")
                          .Options;

            // Run the test against one instance of the context
            using (var context = new CookDbContext(options))
            {
                var service = new EFDishService(context);
                service.CreateDish(dish);
                service.CreateDish(dish2);
                context.SaveChanges();
            }

            // Use a separate instance of the context to verify correct data was saved to database
            using (var context = new CookDbContext(options))
            {
                var service = new EFDishService(context);
                var dishes  = service.GetDishes();

                Assert.Equal(dishes.Count, context.Dish.Count());
            }
        }
Exemplo n.º 7
0
 public void Save()
 {
     _dbContext.SaveChanges();
 }