public void DeleteWorks() { int id = 0; // arrange (use the context directly - we assume that works) var options = new DbContextOptionsBuilder <Project1Context>() .UseInMemoryDatabase("db_pizza_test_delete").Options; using (var db = new Project1Context(options)); // act (for act, only use the repo, to test it) using (var db = new Project1Context(options)) { var repo = new PizzaRepository(db); var ingredientRepository = new IngredientRepository(db); Ingredients ingredient = new Ingredients() { Name = "Ingredient", Stock = 10 }; ingredientRepository.Save(ingredient); ingredientRepository.SaveChanges(); Pizzas pizza = new Pizzas { Name = "Pizza 1", Price = 20, }; pizza.PizzasIngredients.Add(new PizzasIngredients() { IngredientId = ingredient.Id }); repo.Save(pizza); repo.SaveChanges(); id = pizza.Id; } // assert (for assert, once again use the context directly for verify.) using (var db = new Project1Context(options)) { var repo = new PizzaRepository(db); Pizzas pizza = (Pizzas)repo.GetById(id); Assert.NotEqual(0, pizza.Id); Assert.Equal("Pizza 1", pizza.Name); Assert.Equal(20, pizza.Price); repo.Delete(id); repo.SaveChanges(); pizza = (Pizzas)repo.GetById(id); Assert.Null(pizza); } }
public void DeleteWithIdThatDoesntExistThrowsException() { int id = 1000; // arrange (use the context directly - we assume that works) var options = new DbContextOptionsBuilder <Project1Context>() .UseInMemoryDatabase("db_pizza_test_delete").Options; using (var db = new Project1Context(options)); // act (for act, only use the repo, to test it) //using (var db = new Project1Context(options)) //{ //var repo = new PizzaRepository(db); //Pizzas Pizza = new Pizzas { Name = "Test Delete", Stock = 10 }; //repo.Save(Pizza); //repo.SaveChanges(); //id = Pizza.Id; //} // assert (for assert, once again use the context directly for verify.) using (var db = new Project1Context(options)) { var repo = new PizzaRepository(db); Pizzas Pizza = (Pizzas)repo.GetById(id); Assert.Null(Pizza); Assert.Throws <ArgumentException>(() => repo.Delete(id)); } }