public void Can_Send_Pagination_View_Model()
        {
            Mock<IProductRepository> mock = new Mock<IProductRepository>();
            mock.Setup(m => m.Products).Returns(new Product[] {
                new Product {ProductID = 1, Name = "P1"},
                new Product {ProductID = 2, Name = "P2"},
                new Product {ProductID = 3, Name = "P3"},
                new Product {ProductID = 4, Name = "P4"},
                new Product {ProductID = 5, Name = "P5"}
            });

            ProductController controller = new ProductController(mock.Object);
            controller.PageSize = 3;

            ProductsListViewModel result = (ProductsListViewModel)controller.List(null, 2).Model;

            PagingInfo pageInfo = result.PagingInfo;
            List<Product> productsOnPage = result.Products.ToList();
            Assert.AreEqual(pageInfo.CurrentPage, 2);
            Assert.AreEqual(pageInfo.ItemsPerPage, 3);
            Assert.AreEqual(pageInfo.TotalItems, 5);
            Assert.AreEqual(pageInfo.TotalPages, 2);
            Assert.AreEqual(productsOnPage[0].Name, "P4");
            Assert.AreEqual(productsOnPage[1].ProductID, 5);
        }
        public void Can_Paginate()
        {
            var controller = new ProductController(repository) {PageSize = 3};

            var result = (ProductListViewModel) controller.List(null, page: 2).Model;

            Assert.AreEqual(2, result.Products.Count);
            Assert.AreEqual(2, result.PagingInfo.CurrentPage);
            Assert.AreEqual(controller.PageSize, result.PagingInfo.ItemsPerPage);
            Assert.AreEqual(2, result.PagingInfo.TotalPages);
            Assert.AreEqual(repository.Products.Count(), result.PagingInfo.TotalItems);

            Assert.AreEqual("P4", result.Products[0].Name);
            Assert.AreEqual("P5", result.Products[1].Name);
        }
        public void Can_Filter_By_Category()
        {
            var results = new Product[0];

            var controller = new ProductController(repository){PageSize = 3};
            var vm = (controller.List("Cat2", 1).Model as ProductListViewModel);

            results = vm.Products.ToArray();

            Assert.AreEqual("Cat2", vm.CurrentCategory);
            Assert.AreEqual("P2", results[0].Name);
            Assert.AreEqual("Cat2",results[0].Category);
            Assert.AreEqual("P4", results[1].Name);
            Assert.AreEqual("Cat2", results[1].Category);
        }
Ejemplo n.º 4
0
 public void Can_Paginate()
 {
     // Arrange
     Mock<IProductRepository> mock = new Mock<IProductRepository>();
     mock.Setup(m => m.Products).Returns(new Product[] {
         new Product {ProductID = 1, Name = "P1"},
         new Product {ProductID = 2, Name = "P2"},
         new Product {ProductID = 3, Name = "P3"},
         new Product {ProductID = 4, Name = "P4"},
         new Product {ProductID = 5, Name = "P5"}
         });
     ProductController controller = new ProductController(mock.Object);
     controller.PageSize = 3;
     // Act
     ProductsListViewModel result = (ProductsListViewModel)controller.List(null,2).Model;
     // Assert
     Product[] prodArray = result.Products.ToArray();
     Assert.IsTrue(prodArray.Length == 2);
     Assert.AreEqual(prodArray[0].Name, "P4");
     Assert.AreEqual(prodArray[1].Name, "P5");
 }
Ejemplo n.º 5
0
 public void Can_Filter_Products()
 {
     // Arrange
     // - create the mock repository
     Mock<IProductRepository> mock = new Mock<IProductRepository>();
     mock.Setup(m => m.Products).Returns(new Product[] {
         new Product {ProductID = 1, Name = "P1", Category = "Cat1"},
         new Product {ProductID = 2, Name = "P2", Category = "Cat2"},
         new Product {ProductID = 3, Name = "P3", Category = "Cat1"},
         new Product {ProductID = 4, Name = "P4", Category = "Cat2"},
         new Product {ProductID = 5, Name = "P5", Category = "Cat3"}
         });
     // Arrange - create a controller and make the page size 3 items
     ProductController controller = new ProductController(mock.Object);
     controller.PageSize = 3;
     // Action
     Product[] result = ((ProductsListViewModel)controller.List("Cat2", 1).Model)
     .Products.ToArray();
     // Assert
     Assert.AreEqual(result.Length, 2);
     Assert.IsTrue(result[0].Name == "P2" && result[0].Category == "Cat2");
     Assert.IsTrue(result[1].Name == "P4" && result[1].Category == "Cat2");
 }