From 20f1cf9dec78a1e9eb9f7807d7b6c1e892214aa9 Mon Sep 17 00:00:00 2001 From: Jason Zhu Date: Wed, 8 Sep 2021 15:18:46 +1000 Subject: [PATCH] 7.5.1.1 Adding the View Model; UNIT TEST: CREATING PAGE LINKS --- .../SportsStore.UnitTests/UnitTest1.cs | 43 ++++++++++++++++--- 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/SportsStore/SportsStore.UnitTests/UnitTest1.cs b/SportsStore/SportsStore.UnitTests/UnitTest1.cs index b73a060..81b00c3 100644 --- a/SportsStore/SportsStore.UnitTests/UnitTest1.cs +++ b/SportsStore/SportsStore.UnitTests/UnitTest1.cs @@ -2,10 +2,13 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Web.Mvc; using Moq; using SportsStore.Domain.Abstract; using SportsStore.Domain.Entities; using SportsStore.WebUI.Controllers; +using SportsStore.WebUI.HtmlHelpers; +using SportsStore.WebUI.Models; namespace SportsStore.UnitTests { @@ -17,12 +20,13 @@ namespace SportsStore.UnitTests { // Arrange Mock mock = new Mock(); - 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"} + 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; @@ -35,5 +39,32 @@ namespace SportsStore.UnitTests Assert.AreEqual(prodArray[0].Name, "P4"); Assert.AreEqual(prodArray[1].Name, "P5"); } + + [TestMethod] + public void Can_Generate_Page_Links() + { + // Arrange - define an HTML helper - we need to do this + // in order to apply the extension method + HtmlHelper myHelper = null; + // Arrange - create PagingInfo data + PagingInfo pagingInfo = new PagingInfo + { + CurrentPage = 2, + TotalItems = 28, + ItemsPerPage = 10 + }; + + // Arrange - set up the delegate using a lambda expression + Func pageUrlDelegate = i => "Page" + i; + + // Act + MvcHtmlString result = myHelper.PageLinks(pagingInfo, pageUrlDelegate); + + // Assert + Assert.AreEqual(@"1" + + @"2" + + @"3", + result.ToString()); + } } }