From 62e5982ef8768d8c348f241a66d626d808b917a6 Mon Sep 17 00:00:00 2001 From: Jason Zhu Date: Wed, 8 Sep 2021 23:55:32 +1000 Subject: [PATCH] 8.1.4 Correcting the Page Count; UNIT TEST: CATEGORY PRODUCT COUNTS --- .../SportsStore.UnitTests/UnitTest1.cs | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/SportsStore/SportsStore.UnitTests/UnitTest1.cs b/SportsStore/SportsStore.UnitTests/UnitTest1.cs index cc35209..9ba8ad2 100644 --- a/SportsStore/SportsStore.UnitTests/UnitTest1.cs +++ b/SportsStore/SportsStore.UnitTests/UnitTest1.cs @@ -174,5 +174,41 @@ namespace SportsStore.UnitTests // Assert Assert.AreEqual(categoryToSelected, result); } -} + + [TestMethod] + public void Generate_Category_Specific_Product_Count() + { + // Arrange + // - create the mock repository + Mock mock = new Mock(); + 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); + } + } }