public void Can_Paginatie_Category_View_Model() { // Arrange Mock<IProductRepository> mock = new Mock<IProductRepository>(); mock.Setup(m => m.Products).Returns(new[] { new Product {ProductID = 1, Name = "P1", Category = "C1"}, new Product {ProductID = 2, Name = "P2", Category = "C1"}, new Product {ProductID = 3, Name = "P3", Category = "C1"}, new Product {ProductID = 4, Name = "P4", Category = "C1"}, new Product {ProductID = 5, Name = "P5", Category = "C2"}, new Product {ProductID = 6, Name = "P6", Category = "C2"} }); //Arrange var controller = new ProductController(mock.Object); controller.PageSize = 3; //Act Product[] result = ((ProductsListViewModel)controller.List("C1", 1).Model).Products.ToArray(); // Assert Assert.AreEqual(result.Length, 3); Assert.AreEqual(result[0].Name, "P1"); Assert.AreEqual(result[1].Name, "P2"); }
public void Generate_Category_Specific_Product_Count() { // - 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 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); }
public void Can_Paginate() { // Arrane Mock<IProductRepository> mock = new Mock<IProductRepository>(); mock.Setup(m => m.Products).Returns(new[] { 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"}, new Product {ProductID = 6, Name = "P6"} }); var controller = new ProductController(mock.Object); controller.PageSize = 3; //Act //var result = (IEnumerable<Product>)controller.List(2).Model; //if (result == null) throw new ArgumentNullException("result"); ProductsListViewModel result = (ProductsListViewModel)controller.List(null, 2).Model; // Assert Product[] prodArray = result.Products.ToArray(); Assert.IsTrue(prodArray.Length == 3); Assert.AreEqual(prodArray[0].Name, "P4"); Assert.AreEqual(prodArray[1].Name, "P5"); }
public void Can_Send_Pagination_View_Model() { // Arrange Mock<IProductRepository> mock = new Mock<IProductRepository>(); mock.Setup(m => m.Products).Returns(new[] { 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"}, new Product {ProductID = 6, Name = "P6"} }); //Arrange var controller = new ProductController(mock.Object); controller.PageSize = 3; //Act ProductsListViewModel result = (ProductsListViewModel)controller.List(null, 2).Model; // Assert PagingInfo pageInfo = result.PagingInfo; Assert.AreEqual(pageInfo.CurrentPage, 2); Assert.AreEqual(pageInfo.ItemsPerPage, 3); Assert.AreEqual(pageInfo.TotalItems, 6); Assert.AreEqual(pageInfo.TotalPages, 2); }