public void Generate_Category_Specific_Dish_Count()
        {
            //Arrange
            Mock <IDishRepository> mock = new Mock <IDishRepository>();

            mock.Setup(m => m.Dishes).Returns(new List <Dish>
            {
                new Dish {
                    DishId = 1, Name = "Dish1", Category = "Drink"
                },
                new Dish {
                    DishId = 2, Name = "Dish2", Category = "Food"
                },
                new Dish {
                    DishId = 3, Name = "Dish3", Category = "Food"
                },
                new Dish {
                    DishId = 4, Name = "Dish4", Category = "Drink"
                },
                new Dish {
                    DishId = 5, Name = "Dish5", Category = "Food"
                }
            });
            DishController controler = new DishController(mock.Object);

            //Act
            int res1   = ((DishesListViewModel)controler.List("Drink").Model).PagingInfo.TotalItems;
            int res2   = ((DishesListViewModel)controler.List("Food").Model).PagingInfo.TotalItems;
            int resAll = ((DishesListViewModel)controler.List(null).Model).PagingInfo.TotalItems;

            //Assert
            Assert.AreEqual(res1, 2);
            Assert.AreEqual(res2, 3);
            Assert.AreEqual(resAll, 5);
        }
示例#2
0
        public void PagingTest()
        {
            // Arrange
            // Макет репозитория
            var mock = new Mock <IRepository <Dish> >();

            mock.Setup(r => r.GetAll())
            .Returns(new List <Dish>
            {
                new Dish {
                    DishId = 1
                },
                new Dish {
                    DishId = 2
                },
                new Dish {
                    DishId = 3
                },
                new Dish {
                    DishId = 4
                },
                new Dish {
                    DishId = 5
                },
            });
            // Создание объекта контроллера
            var controller = new DishController(mock.Object);

            // Макеты для получения HttpContext и HttpRequest
            var request     = new Mock <HttpRequestBase>();
            var httpContext = new Mock <HttpContextBase>();

            httpContext.Setup(h => h.Request).Returns(request.Object);

            // Создание контекста контроллера
            controller.ControllerContext             = new ControllerContext();
            controller.ControllerContext.HttpContext = httpContext.Object;

            NameValueCollection valueCollection = new NameValueCollection();

            valueCollection.Add("X-Requsted-With", "XMLHttpRequest");
            valueCollection.Add("Accept", "application/json");
            // другой вариант: valueCollection.Add("Accept", "HTML");
            request.Setup(r => r.Headers).Returns(valueCollection);

            // Act
            // Вызов метода List
            var view = controller.List(null, 2) as ViewResult;

            // Assert
            Assert.IsNotNull(view, "Представление не получено");
            Assert.IsNotNull(view.Model, "Модель не получена");
            PageListViewModel <Dish> model = view.Model as PageListViewModel <Dish>;

            Assert.AreEqual(2, model.Count);
            Assert.AreEqual(4, model[0].DishId);
            Assert.AreEqual(5, model[1].DishId);
        }
        public void CategoryTest()
        {
            //Arrange
            //Макет репозитория
            var mock = new Mock <IRepository <Dish> >();

            mock.Setup(r => r.GetAll()).Returns(new List <Dish> {
                new Dish {
                    DishID = 1, GroupName = "1"
                },
                new Dish {
                    DishID = 2, GroupName = "2"
                },
                new Dish {
                    DishID = 3, GroupName = "1"
                },
                new Dish {
                    DishID = 4, GroupName = "2"
                },
                new Dish {
                    DishID = 5, GroupName = "2"
                },
            });
            //Создание объекта контроллера
            var controller = new DishController(mock.Object);

            //Макеты для получения HttpContext HttpRequest
            var request     = new Mock <HttpRequestBase>();
            var httpContext = new Mock <HttpContextBase>();

            httpContext.Setup(h => h.Request).Returns(request.Object);

            //Создание контекста контроллера
            controller.ControllerContext             = new ControllerContext();
            controller.ControllerContext.HttpContext = httpContext.Object;

            //Act
            //Вызов метода List
            var view = controller.List("1", 1) as ViewResult;

            //Assert
            Assert.IsNotNull(view, "Представление не получено");
            Assert.IsNotNull(view.Model, "Модель не получена");
            PageListViewModel <Dish> model = view.Model as PageListViewModel <Dish>;

            Assert.AreEqual(2, model.Count);
            Assert.AreEqual(1, model[0].DishID);
            Assert.AreEqual(3, model[1].DishID);
        }
        public void Can_Send_Pagination_View_Model()
        {
            // Arrange
            Mock <IDishRepository> mock = new Mock <IDishRepository>();

            mock.Setup(m => m.Dishes).Returns(new List <Dish>
            {
                new Dish {
                    DishId = 1, Name = "Dish1"
                },
                new Dish {
                    DishId = 2, Name = "Dish2"
                },
                new Dish {
                    DishId = 3, Name = "Dish3"
                },
                new Dish {
                    DishId = 4, Name = "Dish4"
                },
                new Dish {
                    DishId = 5, Name = "Dish5"
                }
            });
            DishController controller = new DishController(mock.Object);

            controller.pageSize = 3;

            // Act
            DishesListViewModel result = (DishesListViewModel)controller.List(null, 2).Model;

            // Assert
            PagingInfo pageInfo = result.PagingInfo;

            Assert.AreEqual(pageInfo.CurrentPage, 2);
            Assert.AreEqual(pageInfo.ItemsPerPage, 3);
            Assert.AreEqual(pageInfo.TotalItems, 5);
            Assert.AreEqual(pageInfo.TotalPages, 2);
        }
        public void Can_Paginate()
        {
            // Arrange
            Mock <IDishRepository> mock = new Mock <IDishRepository>();

            mock.Setup(m => m.Dishes).Returns(new List <Dish>
            {
                new Dish {
                    DishId = 1, Name = "Dish1"
                },
                new Dish {
                    DishId = 2, Name = "Dish2"
                },
                new Dish {
                    DishId = 3, Name = "Dish3"
                },
                new Dish {
                    DishId = 4, Name = "Dish4"
                },
                new Dish {
                    DishId = 5, Name = "Dish5"
                }
            });
            DishController controller = new DishController(mock.Object);

            controller.pageSize = 3;

            // Act
            DishesListViewModel result = (DishesListViewModel)controller.List(null, 2).Model;

            // Asserts
            List <Dish> dishes = result.Dishes.ToList();

            Assert.IsTrue(dishes.Count == 2);
            Assert.AreEqual(dishes[0].Name, "Dish4");
            Assert.AreEqual(dishes[1].Name, "Dish5");
        }
        public void Can_Filter_Category()
        {
            //Arrange
            Mock <IDishRepository> mock = new Mock <IDishRepository>();

            mock.Setup(m => m.Dishes).Returns(new List <Dish>
            {
                new Dish {
                    DishId = 1, Name = "Dish1", Category = "Drink"
                },
                new Dish {
                    DishId = 2, Name = "Dish2", Category = "Food"
                },
                new Dish {
                    DishId = 3, Name = "Dish3", Category = "Food"
                },
                new Dish {
                    DishId = 4, Name = "Dish4", Category = "Drink"
                },
                new Dish {
                    DishId = 5, Name = "Dish5", Category = "Food"
                }
            });

            DishController controler = new DishController(mock.Object);

            //Act
            DishesListViewModel result = (DishesListViewModel)controler.List("Drink").Model;

            //Assert
            List <Dish> dishes = result.Dishes.ToList();

            Assert.AreEqual(result.CurrentCategory, "Drink");
            Assert.AreEqual(dishes.Count, 2);
            Assert.IsTrue(dishes[0].Name == "Dish1" && dishes[0].Category == "Drink");
            Assert.IsTrue(dishes[1].Name == "Dish4" && dishes[1].Category == "Drink");
        }