示例#1
0
        public void Can_Filter_Products()
        {
            // Arrange
            // - create the mock repository

            Mock<IProductsRepository> mock = new Mock<IProductsRepository>();
            mock.Setup(m => m.Products).Returns(new Product[]
                {
                    new Product {ProductID = 1, Name = "P1", Catagory = "Cat1"},
                    new Product {ProductID = 2, Name = "P2", Catagory = "Cat2"},
                    new Product {ProductID = 3, Name = "P3", Catagory = "Cat1"},
                    new Product {ProductID = 4, Name = "P4", Catagory = "Cat2"},
                    new Product
                        {
                            ProductID = 5,
                            Name = "P5",
                            Catagory = "Cat3"
                        }
                }.AsQueryable());
            // 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].Catagory == "Cat2");
            Assert.IsTrue(result[1].Name == "P4" && result[1].Catagory == "Cat2");
        }
示例#2
0
 public void Can_Send_Pagination_View_Model()
 {
     // Arrange
     // - create the mock repository
     Mock<IProductsRepository> mock = new Mock<IProductsRepository>();
     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"}
         }.AsQueryable());
     // Arrange - create a controller and make the page size 3 items
     ProductController controller = new ProductController(mock.Object);
     controller.PageSize = 3;
     // Action
     ProductsListViewModel result = (ProductsListViewModel) controller.List(null ,2).Model;
     // Assert
     PagingInfo pageInfo = result.PagingInfo;
     Assert.AreEqual(pageInfo.CurrentPages, 2);
     Assert.AreEqual(pageInfo.ItemPerPages, 3);
     Assert.AreEqual(pageInfo.TotalItems, 5);
     Assert.AreEqual(pageInfo.TotalPages, 2);
 }
示例#3
0
 public void Generate_Category_Specific_Product_Count()
 {
     // Arrange
     // - create the mock repository
     Mock<IProductsRepository> mock = new Mock<IProductsRepository>();
     mock.Setup(m => m.Products).Returns(new Product[]
         {
             new Product {ProductID = 1, Name = "P1", Catagory = "Cat1"},
             new Product {ProductID = 2, Name = "P2", Catagory = "Cat2"},
             new Product {ProductID = 3, Name = "P3", Catagory = "Cat1"},
             new Product {ProductID = 4, Name = "P4", Catagory = "Cat2"},
             new Product {ProductID = 5, Name = "P5", Catagory = "Cat3"}
         }.AsQueryable());
       // Arrange - create a controller and make the page size 3 items
     ProductController target = new ProductController(mock.Object);
     target.PageSize = 3;
     // Action - test the product counts for different categories
     int res1 = ((ProductsListViewModel)target.List("Cat1").Model).PagingInfo.TotalItems;
     int res2 = ((ProductsListViewModel)target.List("Cat2").Model).PagingInfo.TotalItems;
     int res3 = ((ProductsListViewModel)target.List("Cat3").Model).PagingInfo.TotalItems;
     int resAll = ((ProductsListViewModel)target.List(null).Model).PagingInfo.TotalItems;
     // Assert
     Assert.AreEqual(res1, 2);
     Assert.AreEqual(res2, 2);
     Assert.AreEqual(res3, 1);
     Assert.AreEqual(resAll, 5);
 }
示例#4
0
        public void Can_Paginate()
        {
            //Arrange
            // - create the mock reposortry
            Mock<IProductsRepository> mock  = new Mock<IProductsRepository>();
            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"}
                }.AsQueryable());

            // create a controller and make the page size 3 items
            ProductController controller = new ProductController(mock.Object);
            controller.PageSize = 3;

            // Action

            IEnumerable<Product> result = (IEnumerable<Product>)controller.List(null ,2).Model;
            // Assert
            Product[] prodArray = result.ToArray();
            Assert.IsTrue(prodArray.Length == 2);
            Assert.AreEqual(prodArray[0].Name, "P4");
            Assert.AreEqual(prodArray[1].Name, "P5");
        }