Пример #1
0
        public void TestPages()
        {
            //Arange
            Mock <IProductRepository> mock = new Mock <IProductRepository>();

            mock.Setup(p => p.Products).Returns(
                new Product[]
            {
                new Product {
                    ProductID = 1, Name = "P1", Category = "C1"
                },
                new Product {
                    ProductID = 2, Name = "P2", Category = "C2"
                },
                new Product {
                    ProductID = 3, Name = "P3", Category = "C2"
                },
                new Product {
                    ProductID = 4, Name = "P4", Category = "C1"
                },
                new Product {
                    ProductID = 5, Name = "P5", Category = "C1"
                },
                new Product {
                    ProductID = 6, Name = "P6", Category = "C3"
                },
                new Product {
                    ProductID = 7, Name = "P7", Category = "C4"
                },
                new Product {
                    ProductID = 8, Name = "P8", Category = "C5"
                },
                new Product {
                    ProductID = 9, Name = "P9", Category = "C2"
                },
                new Product {
                    ProductID = 10, Name = "P10", Category = "C1"
                }
            }.AsQueryable()
                );
            ProductController controller = new ProductController(mock.Object);

            controller.pageSize = 3;
            //Act
            ProductListPageInfoToView result  = (ProductListPageInfoToView)controller.List("C2", 1).Model;
            ProductListPageInfoToView result2 = (ProductListPageInfoToView)controller.List(null, 1).Model;

            Product[] prodArray = result.Products.ToArray();
            //Assert
            Assert.AreEqual(result.PageInfo.TotalPage(), 1);
            Assert.AreEqual(result2.PageInfo.TotalPage(), 4);
        }
Пример #2
0
        // GET: Product
        public ViewResult List(string category, int page = 1)
        {
            ProductListPageInfoToView model = new ProductListPageInfoToView
            {
                Products = repository.Products.Select(x => x).Where(x => category == null || x.Category == category).OrderBy(e => e.ProductID).Skip((page - 1) * pageSize).Take(pageSize),
                PageInfo = new PageInfo
                {
                    CurrentPage  = page,
                    ItemsPerPage = pageSize,
                    TotalItems   = (category != null) ? repository.Products.Select(x => x).Where(x => x.Category == category).Count()
                    : repository.Products.Select(x => x).Count()
                },
                Category = category
            };

            return(View(model));
        }