Exemple #1
0
        public void Products_ReturnsProductsList_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 = 2, Name = "N2", Description = "D2", Price = 11, ProductCategoryName = "C1", ProductCategoryId = 1
                },
                new ProductApiDTO {
                    ProductId = 3, Name = "N3", Description = "D3", Price = 12, ProductCategoryName = "C1", ProductCategoryId = 1
                },
                new ProductApiDTO {
                    ProductId = 4, Name = "N4", Description = "D4", Price = 13, ProductCategoryName = "C1", ProductCategoryId = 1
                },
                new ProductApiDTO {
                    ProductId = 5, Name = "N5", Description = "D5", Price = 14, 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 = 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>());
            productCategoryMock.Setup(repo => repo.Name).Returns("C1");
            productCategoryMock.Setup(repo => repo.ProductCategoryId).Returns(1);
            var controller = new ProductApiController(mock.Object);

            // act
            var result = controller.Products();

            // assert
            mock.Verify();
            Assert.Equal(productsDTO[0].ProductId, result.Value[0].ProductId);
            Assert.Equal(productsDTO[0].Description, result.Value[0].Description);
            Assert.Equal(productsDTO[0].ProductCategoryName, result.Value[0].ProductCategoryName);
        }
Exemple #2
0
        public void Products_ReturnsProductsList_NotFound()
        {
            // arrange
            var mock = new Mock <EFProductRepository>();
            var productCategoryMock = new Mock <ProductCategory>();
            var controller          = new ProductApiController(mock.Object);

            // act
            var result = controller.Products();

            // assert
            Assert.IsType <NotFoundResult>(result.Result);
        }