8.1.3.2 Generating Category Lists; UNIT TEST: GENERATING THE CATEGORY LIST

chap08
Jason Zhu 2021-09-08 23:00:27 +10:00
parent d240d7d569
commit c55dd5f133
1 changed files with 35 additions and 7 deletions

View File

@ -72,12 +72,13 @@ namespace SportsStore.UnitTests
// Arrange // Arrange
Mock<IProductRepository> mock = new Mock<IProductRepository>(); Mock<IProductRepository> mock = new Mock<IProductRepository>();
mock.Setup(m => m.Products).Returns(new Product[] { mock.Setup(m => m.Products).Returns(new Product[]
new Product {ProductID = 1, Name = "P1"}, {
new Product {ProductID = 2, Name = "P2"}, new Product { ProductID = 1, Name = "P1" },
new Product {ProductID = 3, Name = "P3"}, new Product { ProductID = 2, Name = "P2" },
new Product {ProductID = 4, Name = "P4"}, new Product { ProductID = 3, Name = "P3" },
new Product {ProductID = 5, Name = "P5"} new Product { ProductID = 4, Name = "P4" },
new Product { ProductID = 5, Name = "P5" }
}); });
// Arrange // Arrange
@ -121,5 +122,32 @@ namespace SportsStore.UnitTests
Assert.IsTrue(result[0].Name == "P2" && result[0].Category == "Cat2"); Assert.IsTrue(result[0].Name == "P2" && result[0].Category == "Cat2");
Assert.IsTrue(result[1].Name == "P4" && result[1].Category == "Cat2"); Assert.IsTrue(result[1].Name == "P4" && result[1].Category == "Cat2");
} }
}
[TestMethod]
public void Can_Create_Categories()
{
// Arrange
// - 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 = "Apples" },
new Product { ProductID = 2, Name = "P2", Category = "Apples" },
new Product { ProductID = 3, Name = "P3", Category = "Plums" },
new Product { ProductID = 4, Name = "P4", Category = "Oranges" },
});
// Arrange - create the controller
NavController target = new NavController(mock.Object);
// Act = get the set of categories
string[] results = ((IEnumerable<string>)target.Menu().Model).ToArray();
// Assert
Assert.AreEqual(results.Length, 3);
Assert.AreEqual(results[0], "Apples");
Assert.AreEqual(results[1], "Oranges");
Assert.AreEqual(results[2], "Plums");
}
}
} }