Example #1
0
        public void CanUpdatePantry()
        {
            // setup
            MyContext             mc               = new MyContext();
            MockIFoodRepository   foodRepository   = new MockIFoodRepository(mc);
            MockIPantryRepository pantryRepository = new MockIPantryRepository(mc);
            FoodController        controller       = new FoodController(foodRepository, pantryRepository);
            Pantry p = new Pantry {
                PantryID = 0, Units = 35
            };

            // execute food update
            // have to create a new controller context and make the httpcontext equal fakehttpcontext so the controller can
            // use the session when testing
            controller.ControllerContext = new System.Web.Mvc.ControllerContext()
            {
                HttpContext = FakeHttpContext()
            };
            controller.update(0);
            controller.update(p);

            // assert food has been updated
            p = pantryRepository.Pantrys.Select(x => x).Where(x => x.PantryID == 0).First();
            Assert.AreEqual(35, p.Units);
        }
Example #2
0
        public void CanDeleteFood()
        {
            // setup
            MyContext             mc               = new MyContext();
            MockIFoodRepository   foodRepository   = new MockIFoodRepository(mc);
            MockIPantryRepository pantryRepository = new MockIPantryRepository(mc);
            FoodController        controller       = new FoodController(foodRepository, pantryRepository);
            int id = 1;

            // execute delete
            controller.delete(id);

            // assert food is removed
            //if null food was removed from user pantry
            Assert.IsNull(pantryRepository.Pantrys.Select(x => x).Where(x => x.FoodID == id).FirstOrDefault());
        }
Example #3
0
        public void CanAddFood()
        {
            // setup
            MyContext             mc               = new MyContext();
            MockIFoodRepository   foodRepository   = new MockIFoodRepository(mc);
            MockIPantryRepository pantryRepository = new MockIPantryRepository(mc);
            FoodController        controller       = new FoodController(foodRepository, pantryRepository);
            User user = new User()
            {
                UserID = 1
            };

            // execute add food
            controller.addFoods(user, 1);

            // assert food is added to pantry
            //if not null then it is found in pantry
            Assert.IsNotNull(pantryRepository.Pantrys.Where(x => x.FoodID == 1).First());
        }
Example #4
0
        public void CannotAddFoodWithNameAlreadyInDatabase()
        {
            // setup
            MyContext             mc               = new MyContext();
            MockIFoodRepository   foodRepository   = new MockIFoodRepository(mc);
            MockIPantryRepository pantryRepository = new MockIPantryRepository(mc);
            FoodController        controller       = new FoodController(foodRepository, pantryRepository);

            // execute createNewFood
            controller.createNewFood(new User()
            {
                UserID = 0
            }, new Food()
            {
                Name = "Food 1"
            });

            // assert food wasn't added
            Assert.IsTrue(foodRepository.Foods.Select(x => x).Where(x => x.Name.Equals("Food 1")).Count() == 1);
        }
Example #5
0
        public void CanAddNewFoodToDatabase()
        {
            // setup
            MyContext             mc               = new MyContext();
            MockIFoodRepository   foodRepository   = new MockIFoodRepository(mc);
            MockIPantryRepository pantryRepository = new MockIPantryRepository(mc);
            FoodController        controller       = new FoodController(foodRepository, pantryRepository);

            // execute createNewFood
            controller.createNewFood(new User()
            {
                UserID = 0
            }, new Food()
            {
                Name = "Rice"
            });

            // assert food has been added to database and pantry
            // if not null if found the food object with name rice
            Food f = foodRepository.Foods.Select(x => x).Where(x => x.Name.Equals("Rice")).FirstOrDefault();

            Assert.IsNotNull(f);
            Assert.IsNotNull(pantryRepository.Pantrys.Select(x => x).Where(x => x.UserID == 0 && x.FoodID == f.FoodID));
        }