public void Can_Filter_Products() { // 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 = "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 controller = new ProductController(mock.Object); controller.PageSize = 3; // Act ViewResult vr = controller.List("Cat2", 1) as ViewResult; Product[] result = ((ProductsListViewModel)vr.Model).Products.ToArray(); // Assert Assert.AreEqual(result.Length, 2); Assert.IsTrue(result[0].Name == "P2" && result[0].Category == "Cat2"); Assert.IsTrue(result[1].Name == "P4" && result[1].Category == "Cat2"); }
public void Can_Paginate() { // 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"}, 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()); // create a controller and make the page size 3 items ProductController controller = new ProductController(mock.Object); controller.PageSize = 3; // Action ProductsListViewModel result = (ProductsListViewModel)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 Cannot_Retrieve_Image_Data_For_Invalid_ID() { // 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"}, new Product {ProductID = 2, Name = "P2"} }.AsQueryable()); // Arrange - create the controller ProductController target = new ProductController(mock.Object); // Act - call the GetImage action method ActionResult result = target.GetImage(100); // Assert Assert.IsNull(result); }
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); controller.PageSize = 3; // Act ViewResult vr = controller.List(null,2) as ViewResult; ProductsListViewModel result = (ProductsListViewModel)vr.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_Filter_Products() { //Arrange Mock<IProductsRepository> mock = new Mock<IProductsRepository>(); 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"} }); ProductController controller = new ProductController(mock.Object); controller.PageSize = 3; Product[] result = ((ProductsListViewModel) controller.List("Cat2", 1).Model).Products.ToArray(); //Assert Assert.AreEqual(result.Length, 2); Assert.IsTrue(result[0].Name == "P2" && result[0].Category == "Cat2"); Assert.IsTrue(result[1].Name == "P4" && result[1].Category == "Cat2"); }
public void Can_Retrieve_Image_Data() { // Arrange - create a Product with image data Product prod = new Product { ProductID = 2, Name = "Test", ImageData = new byte[] { }, ImageMimeType = "image/png" }; // 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"}, prod, new Product {ProductID = 3, Name = "P3"} }.AsQueryable()); // Arrange - create the controller ProductController target = new ProductController(mock.Object); // Act - call the GetImage action method ActionResult result = target.GetImage(2); // Assert Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(FileResult)); Assert.AreEqual(prod.ImageMimeType, ((FileResult)result).ContentType); }
public void Generate_Category_Specific_Product_Count() { // 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 = "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; // Act ViewResult vr = target.List("Cat1") as ViewResult; int res1 = ((ProductsListViewModel)vr.Model).PagingInfo.TotalItems; vr = target.List("Cat2") as ViewResult; int res2 = ((ProductsListViewModel)vr.Model).PagingInfo.TotalItems; vr = target.List("Cat3") as ViewResult; int res3 = ((ProductsListViewModel)vr.Model).PagingInfo.TotalItems; vr = target.List(null) as ViewResult; int resAll = ((ProductsListViewModel)vr.Model).PagingInfo.TotalItems; // Assert Assert.AreEqual(res1, 2); Assert.AreEqual(res2, 2); Assert.AreEqual(res3, 1); Assert.AreEqual(resAll, 5); }
public void Can_Send_Pagination_View_Model() { // 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"} }); // Arrange ProductController controller = new ProductController(mock.Object); controller.PageSize = 3; // Act ViewResult vr = controller.List(null, 2) as ViewResult; ProductsListViewModel result = (ProductsListViewModel)vr.Model; // Assert 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_Send_Pagination_View_Model() { // 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"}, 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()); // Arrange - create a controller and make the page size 3 items ProductController controller = new ProductController(mock.Object); controller.PageSize = 3; // Action 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, 5); Assert.AreEqual(pageInfo.TotalPages, 2); }
public void Generate_Specific_Product_Count() { //Arrange Mock<IProductsRepository> mock = new Mock<IProductsRepository>(); 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"} }); ProductController target = new ProductController(mock.Object); target.PageSize = 3; //action int cat1Count = ((ProductsListViewModel) target.List("Cat1").Model).PagingInfo.TotalItems; int cat2Count = ((ProductsListViewModel) target.List("Cat2").Model).PagingInfo.TotalItems; int cat3Count = ((ProductsListViewModel) target.List("Cat3").Model).PagingInfo.TotalItems; int countAll = ((ProductsListViewModel) target.List(null).Model).PagingInfo.TotalItems; //assert Assert.AreEqual(cat1Count, 2); Assert.AreEqual(cat2Count, 2); Assert.AreEqual(cat3Count, 1); Assert.AreEqual(countAll, 5); }