public void Can_Retrieve_Image_Data() { // Arrange - create a Product with image data Product prod = new Product { ProductId = 2, ProductName = "Test", ImageData = new byte[] { }, ImageMimeType = "image/png" }; // Arrange - create the mock repository Mock<IShoeStoreRepository> mock = new Mock<IShoeStoreRepository>(); mock.Setup(m => m.Products).Returns(new Product[] { new Product {ProductId = 1, ProductName = "P1"}, prod, new Product {ProductId = 3, ProductName = "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 Cannot_Retrieve_Image_Data_For_Invalid_ID() { // Arrange - create the mock repository Mock<IShoeStoreRepository> mock = new Mock<IShoeStoreRepository>(); mock.Setup(m => m.Products).Returns(new Product[] { new Product {ProductId = 1, ProductName = "P1"}, new Product {ProductId = 2, ProductName = "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_Send_Pagination_View_Model() { // Arrange Mock<IShoeStoreRepository> mock = new Mock<IShoeStoreRepository>(); mock.Setup(m => m.Products).Returns(new Product[] { new Product {ProductId = 1, ProductName = "P1"}, new Product {ProductId = 2, ProductName = "P2"}, new Product {ProductId = 3, ProductName = "P3"}, new Product {ProductId = 4, ProductName = "P4"}, new Product {ProductId = 5, ProductName = "P5"} }); // Arrange ProductController controller = new ProductController(mock.Object); controller.PageSize = 3; // Act ProductsListViewModel result = (ProductsListViewModel)controller.List(0, 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 Can_Paginate() { // Arrange Mock<IShoeStoreRepository> mock = new Mock<IShoeStoreRepository>(); mock.Setup(m => m.Products).Returns(new Product[] { new Product {ProductId = 1, ProductName = "P1"}, new Product {ProductId = 2, ProductName = "P2"}, new Product {ProductId = 3, ProductName = "P3"}, new Product {ProductId = 4, ProductName = "P4"}, new Product {ProductId = 5, ProductName = "P5"} }); ProductController controller = new ProductController(mock.Object); controller.PageSize = 3; // Act //IEnumerable<Product> result = (IEnumerable<Product>)controller.List(2).Model; ProductsListViewModel result = (ProductsListViewModel)controller.List(0, 2).Model; // Assert //Product[] prodArray = result.ToArray(); Product[] prodArray = result.Products.ToArray(); Assert.IsTrue(prodArray.Length == 2); Assert.AreEqual(prodArray[0].ProductName, "P4"); Assert.AreEqual(prodArray[1].ProductName, "P5"); }
public void Can_Filter_Products() { // Arrange // - create the mock repository Mock<IShoeStoreRepository> mock = new Mock<IShoeStoreRepository>(); mock.Setup(m => m.Products).Returns(new Product[] { new Product {ProductId = 1, ProductName = "P1", ProductCategoryId = 1}, new Product {ProductId = 2, ProductName = "P2", ProductCategoryId = 2}, new Product {ProductId = 3, ProductName = "P3", ProductCategoryId = 1}, new Product {ProductId = 4, ProductName = "P4", ProductCategoryId = 2}, new Product {ProductId = 5, ProductName = "P5", ProductCategoryId = 3} }); // Arrange - create a controller and make the page size 3 items ProductController controller = new ProductController(mock.Object); controller.PageSize = 3; // Action Product[] result = ((ProductsListViewModel)controller.List(2, 1).Model) .Products.ToArray(); // Assert Assert.AreEqual(result.Length, 2); Assert.IsTrue(result[0].ProductName == "P2" && result[0].ProductCategoryId == 2); Assert.IsTrue(result[1].ProductName == "P4" && result[1].ProductCategoryId == 2); }
public void Generate_Category_Specific_Product_Count() { // Arrange // - create the mock repository Mock<IShoeStoreRepository> mock = new Mock<IShoeStoreRepository>(); mock.Setup(m => m.Products).Returns(new Product[] { new Product {ProductId = 1, ProductName = "P1", ProductCategoryId = 1}, new Product {ProductId = 2, ProductName = "P2", ProductCategoryId = 2}, new Product {ProductId = 3, ProductName = "P3", ProductCategoryId = 1}, new Product {ProductId = 4, ProductName = "P4", ProductCategoryId = 2}, new Product {ProductId = 5, ProductName = "P5", ProductCategoryId = 3} }); // 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(1).Model).PagingInfo.TotalItems; int res2 = ((ProductsListViewModel)target.List(2).Model).PagingInfo.TotalItems; int res3 = ((ProductsListViewModel)target.List(3).Model).PagingInfo.TotalItems; int resAll = ((ProductsListViewModel)target.List(0).Model).PagingInfo.TotalItems; // Assert Assert.AreEqual(res1, 2); Assert.AreEqual(res2, 2); Assert.AreEqual(res3, 1); Assert.AreEqual(resAll, 5); }