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" }, }).AsQueryable() ); ProductController controller = new ProductController(mock.Object) { PageSize = 3 }; ProductListVIewModel result = ((ViewResult)controller.List(null, 2)).ViewData.Model as ProductListVIewModel; PagingInfo pageinfo = result.Page_info; Assert.Equal(2, pageinfo.CurrentPage); Assert.Equal(3, pageinfo.ItemsPerPage); Assert.Equal(5, pageinfo.TotalItems); Assert.Equal(2, pageinfo.TotalPages); }
public void Can_Paginate() {//opganizacia Mock <IProductRepository> mock = new Mock <IProductRepository>(); mock.Setup(m => m.Products).Returns(((IEnumerable <Product>)(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() /*as IQueryable<Product>*/); ProductController curent = new ProductController(mock.Object); curent.PageSize = 3; ProductListVIewModel result = (curent.List(null, 2) as ViewResult).ViewData.Model as ProductListVIewModel; Product[] array = result.Products.ToArray <Product>(); Assert.True(array.Length == 2); Assert.Equal("P4", array[0].Name); Assert.Equal("P5", array[1].Name); }
public ViewResult List(string category, int page = 1) { ProductListVIewModel model = new ProductListVIewModel { Products = repository.Products .Where(p => category == null || p.Category == category) .OrderBy(product => product.ProductID) .Skip((page - 1) * PageSize) .Take(PageSize), _PagingInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = category == null?repository.Products.Count() : repository.Products.Where(f => f.Category == category).Count() }, CurrentCategory = category }; return(View(model)); }
public void Can_Send_Pagination_View_Model() { // Arrange Mock <IProductRepository> mock = new Mock <IProductRepository>(); mock.Setup(m => m.Products).Returns((new Product[] { new Product { Id = 1, Name = "P1" }, new Product { Id = 2, Name = "P2" }, new Product { Id = 3, Name = "P3" }, new Product { Id = 4, Name = "P4" }, new Product { Id = 5, Name = "P5" } }).AsQueryable <Product>()); // Arrange ProductController controller = new ProductController(mock.Object) { pagesize = 3 }; // Act ProductListVIewModel result = controller.List(null, 2).ViewData.Model as ProductListVIewModel; // Assert PageInfo pageInfo = result.PageInfo; Assert.Equal(2, pageInfo.CurrentPage); Assert.Equal(3, pageInfo.ItemsPerPage); Assert.Equal(5, pageInfo.TotalItems); Assert.Equal(2, pageInfo.TotalPages); }
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) { PageSize = 3 }; //Act ProductListVIewModel result = (ProductListVIewModel)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"); }
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) { PageSize = 3 }; ProductListVIewModel result = (ProductListVIewModel)controller.List(null, 2).Model; 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() { Mock <IProductRepository> mock = new Mock <IProductRepository>(); mock.Setup(m => m.Products).Returns((new Product[] { new Product { Id = 1, Name = "P1" }, new Product { Id = 2, Name = "P2" }, new Product { Id = 3, Name = "P3" }, new Product { Id = 4, Name = "P4" }, new Product { Id = 5, Name = "P5" } }).AsQueryable <Product>()); ProductController controller = new ProductController(mock.Object); controller.pagesize = 3; ProductListVIewModel result = controller.List(null, 2).ViewData.Model as ProductListVIewModel; Product[] prodArray = result.Products.ToArray(); Assert.True(prodArray.Length == 2); Assert.Equal("P4", prodArray[0].Name); Assert.Equal("P5", prodArray[1].Name); }