예제 #1
0
        public async void GetProductByIdTest(int id, string name)
        {
            Mock <IProductRepository> mock = new Mock <IProductRepository>();

            mock.Setup(m => m.Products).Returns(new Product[] {
                new Product {
                    ID = 1, Name = "P1", Category = "C1"
                },
                new Product {
                    ID = 2, Name = "P2", Category = "C1"
                },
                new Product {
                    ID = 3, Name = "P3", Category = "C2"
                },
                new Product {
                    ID = 4, Name = "P4", Category = "C2"
                },
            }.AsQueryable());

            ProductsAPIController controller = new ProductsAPIController(mock.Object);
            var okResult = (await controller.GetProductById(id)) as OkObjectResult;
            var result   = okResult.Value as Product;

            Assert.Equal(result.Name, name);
        }
예제 #2
0
        public async void GetProductsByCategoryTest()
        {
            Mock <IProductRepository> mock = new Mock <IProductRepository>();

            mock.Setup(m => m.Products).Returns(new Product[] {
                new Product {
                    ID = 1, Name = "P1", Category = "C1"
                },
                new Product {
                    ID = 2, Name = "P2", Category = "C1"
                },
                new Product {
                    ID = 3, Name = "P3", Category = "C2"
                },
                new Product {
                    ID = 4, Name = "P4", Category = "C2"
                },
            }.AsQueryable());

            ProductsAPIController controller = new ProductsAPIController(mock.Object);
            var okResult = (await controller.GetProductsByCategory("C1")) as OkObjectResult;
            var result   = okResult.Value as IEnumerable <Product>;

            Assert.Equal(2, result.Count());
            Assert.Equal("C1", result.ElementAt(0).Category);
            Assert.Equal("C1", result.ElementAt(1).Category);
            Assert.Contains(result, x => x.Name == "P1");
            Assert.Contains(result, x => x.Name == "P2");
        }
        public ProductAPIControllerTest()
        {
            _mockRepo      = new Mock <IRepository <Product> >();
            _apiController = new ProductsAPIController(_mockRepo.Object);

            products = new List <Product>()
            {
                new Product()
                {
                    Color = "Grey", Id = 3, Name = "OstrichToy", Price = 85.12m, Stock = 133
                }, new Product()
                {
                    Color = "Blue", Id = 11, Name = "FiberPen", Price = 12m, Stock = 1500
                }
            };
        }