Ejemplo n.º 1
0
        public async Task CategoriesPerProduct_Called_ReturnsExpectedCategories()
        {
            // Arrange
            Category category1 = new Category()
            {
                CategoryID = 1, Name = "cat1"
            };
            Category category2 = new Category()
            {
                CategoryID = 2, Name = "cat2"
            };
            Category category3 = new Category()
            {
                CategoryID = 3, Name = "cat3"
            };
            Product fakeProduct1 = new Product()
            {
                Categories = new List <Category>()
                {
                    category1, category2
                }
            };
            List <Category> fakeCategoryList = new List <Category>()
            {
                category1, category2, category3
            };

            _productRepository.Setup(p => p.GetByIDAsync(1)).ReturnsAsync(fakeProduct1);
            _categoryRepository.Setup(p => p.GetAllAsync()).ReturnsAsync(fakeCategoryList);

            ProductController controller = new ProductController(_productRepository.Object, _categoryRepository.Object, _orderRepository.Object);
            // Act
            JsonResult result = await controller.CategoriesPerProduct(1) as JsonResult;

            List <ProductCategoryViewModel> resultPCVM = result.Data as List <ProductCategoryViewModel>;

            //Assert
            Assert.IsTrue(resultPCVM.Count == 2);
            Assert.IsTrue(resultPCVM[0].CategoryName == "cat1");
            Assert.IsTrue(resultPCVM[1].CategoryName == "cat2");
        }