コード例 #1
0
        public void GetByNameThatDoesntExistsReturnsNull()
        {
            string name = "Not existing name";

            // arrange (use the context directly - we assume that works)
            var options = new DbContextOptionsBuilder <Project1Context>()
                          .UseInMemoryDatabase("db_pizza_test_getByName_List_Wrong").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  = "Name Pizza 1",
                    Price = 20,
                };
                repo.Save(Pizza);

                Pizza = new Pizzas
                {
                    Name  = "Name Pizza 2",
                    Price = 20,
                };
                repo.Save(Pizza);

                Pizza = new Pizzas
                {
                    Name  = "Name Pizza 3",
                    Price = 20,
                };
                repo.Save(Pizza);

                repo.SaveChanges();
            }

            // assert (for assert, once again use the context directly for verify.)
            using (var db = new Project1Context(options))
            {
                var           repo = new PizzaRepository(db);
                List <Pizzas> list = (List <Pizzas>)repo.GetByName(name);

                Assert.Empty(list);
            }
        }
コード例 #2
0
        public void GetByNameWorks()
        {
            List <Pizzas> inserted  = new List <Pizzas>();
            string        name      = "Test";
            string        wrongName = "Name";
            int           id        = 0;

            // arrange (use the context directly - we assume that works)
            var options = new DbContextOptionsBuilder <Project1Context>()
                          .UseInMemoryDatabase("db_pizza_test_getByName_List").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  = "Test Pizza 1",
                    Price = 20,
                };
                Pizza.PizzasIngredients.Add(new PizzasIngredients()
                {
                    IngredientId = ingredient.Id
                });
                repo.Save(Pizza);
                inserted.Add(Pizza);

                Pizza = new Pizzas
                {
                    Name  = "Name Pizza 2",
                    Price = 20,
                };
                Pizza.PizzasIngredients.Add(new PizzasIngredients()
                {
                    IngredientId = ingredient.Id
                });
                repo.Save(Pizza);
                inserted.Add(Pizza);

                Pizza = new Pizzas
                {
                    Name  = "Test Pizza 3",
                    Price = 20,
                };
                Pizza.PizzasIngredients.Add(new PizzasIngredients()
                {
                    IngredientId = ingredient.Id
                });
                repo.Save(Pizza);
                inserted.Add(Pizza);

                repo.SaveChanges();
            }

            // assert (for assert, once again use the context directly for verify.)
            using (var db = new Project1Context(options))
            {
                var           repo = new PizzaRepository(db);
                List <Pizzas> list = (List <Pizzas>)repo.GetByName(name);

                Assert.Equal(2, list.Count);

                foreach (Pizzas Pizza in list)
                {
                    Assert.NotEqual(0, Pizza.Id);
                    Assert.DoesNotContain(wrongName, Pizza.Name);
                    Assert.Contains(name, Pizza.Name);
                    Assert.Equal(20, Pizza.Price);
                }
            }
        }