Example #1
0
        public void ProductsList_ReturnsProductsItemByCategory_NotFound()
        {
            // arrange
            var mock = new Mock <EFProductRepository>();
            var productCategoryMock = new Mock <ProductCategory>();

            var products = new Product[] {
                new Product {
                    ProductId = 1, Name = "N1", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 1
                },
                new Product {
                    ProductId = 2, Name = "N2", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 1
                },
                new Product {
                    ProductId = 3, Name = "N3", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 1
                },
                new Product {
                    ProductId = 4, Name = "N4", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 1
                },
                new Product {
                    ProductId = 5, Name = "N5", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 1
                }
            };

            mock.Setup(repo => repo.GetProducts()).Returns(products.AsQueryable <Product>());

            var controller = new ProductApiController(mock.Object);

            // act
            var result = controller.ProductsList(123);

            // assert
            Assert.IsType <NotFoundResult>(result.Result);
            Assert.Null(result.Value);
        }
Example #2
0
        public void ProductsList_ReturnsProductsItemByCategory_Ok()
        {
            // arrange
            var mock = new Mock <EFProductRepository>();
            var productCategoryMock = new Mock <ProductCategory>();

            var productsDTO = new ProductApiDTO[] {
                new ProductApiDTO {
                    ProductId = 1, Name = "N1", Description = "D1", Price = 10, ProductCategoryName = "C1", ProductCategoryId = 1
                },
                new ProductApiDTO {
                    ProductId = 3, Name = "N3", Description = "D3", Price = 12, ProductCategoryName = "C1", ProductCategoryId = 1
                },
            };

            var products = new Product[] {
                new Product {
                    ProductId = 1, Name = "N1", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 1
                },
                new Product {
                    ProductId = 2, Name = "N2", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 2
                },
                new Product {
                    ProductId = 3, Name = "N3", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 1
                },
                new Product {
                    ProductId = 4, Name = "N4", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 3
                },
                new Product {
                    ProductId = 5, Name = "N5", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 4
                }
            };

            mock.Setup(repo => repo.GetProducts()).Returns(products.AsQueryable <Product>());
            productCategoryMock.Setup(repo => repo.Name).Returns("C1");
            productCategoryMock.Setup(repo => repo.ProductCategoryId).Returns(1);
            var controller = new ProductApiController(mock.Object);

            // act
            var result = controller.ProductsList(1);

            // assert
            Assert.Equal(result.Value.ToList().Count, 2);
            Assert.Equal(productsDTO[0].ProductCategoryId, result.Value[0].ProductCategoryId);
            Assert.Equal(productsDTO[1].ProductCategoryId, result.Value[1].ProductCategoryId);
            Assert.Equal(productsDTO[0].ProductCategoryName, result.Value[0].ProductCategoryName);
            Assert.Equal(productsDTO[1].ProductCategoryName, result.Value[1].ProductCategoryName);
            Assert.Equal(productsDTO[0].ProductId, result.Value[0].ProductId);
            Assert.Equal(productsDTO[1].ProductId, result.Value[1].ProductId);
        }