예제 #1
0
        public async Task CategoriesNotInproduct_Called_ReturnsExpectedCategoriesWhichAreNotForProduct()
        {
            // Arrange
            //Setup their relationship
            Product fakeProduct1 = new Product()
            {
                ProductID = 1, Categories = new List <Category>()
                {
                }
            };
            Category category1 = new Category()
            {
                CategoryID = 1, Name = "cat1", Products = new List <Product>()
                {
                    fakeProduct1
                }
            };
            Category category2 = new Category()
            {
                CategoryID = 2, Name = "cat2", Products = new List <Product>()
                {
                    fakeProduct1
                }
            };
            Category category3 = new Category()
            {
                CategoryID = 3, Name = "cat3", Products = new List <Product>()
                {
                }
            };

            fakeProduct1 = new Product()
            {
                ProductID = 1, 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.CategoriesNotInProduct(1) as JsonResult;

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

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