public void GetProducts_When_Filtered_By_Ids_Returns_CorrectSet() { // given there are products with suppliers and categories var supplier = new Supplier { SupplierID = 1, CompanyName = "Supplier 1" }; var category = new Category { CategoryID = 1, CategoryName = "Category 1" }; var products = new List<Product>(); for (var i = 0; i < 100; i++) { var product = new Product { ProductID = i, ProductName = $"Product {i}", Category = category, Supplier = supplier }; products.Add(product); } var mockedUnitOfWork = new Mock<IUnitOfWork>(); var mockedProductRepository = new Mock<IRepository<Product>>(); mockedUnitOfWork.Setup(e => e.GetRepository<Product>()).Returns(mockedProductRepository.Object); mockedProductRepository.Setup(e => e.GetAll()).Returns(products.AsQueryable()); // when GetProducts is called with id filter var productsService = new ProductsService(mockedUnitOfWork.Object); var getProductsFilter = new GetProductsFilter {Ids = new[] {1, 7, 15, 22, 9}}; var productDescriptions = productsService.GetProducts(Int32.MaxValue, 1, getProductsFilter).Data; // then correct set of product descriptions are returned Assert.IsTrue(productDescriptions.Select(e => e.Id).Intersect(new[] { 1, 7, 15, 22, 9 }).Count() == 5, "Products with ids 1, 7, 15, 22, 9 are returned"); }
public void GetSupplierDescriptions_returns_all_supplier_descriptions() { // Given 100 suppliers var supplierList = new List<Supplier>(); for (var i = 0; i < 100; i++) { var supplier = new Supplier { SupplierID = i, CompanyName = $"Supplier {i}" }; supplierList.Add(supplier); } var mockedUnitOfWork = new Mock<IUnitOfWork>(); var mockedSupplierRepository = new Mock<IRepository<Supplier>>(); mockedUnitOfWork.Setup(e => e.GetRepository<Supplier>()).Returns(mockedSupplierRepository.Object); mockedSupplierRepository.Setup(e => e.GetAll()).Returns(supplierList.AsQueryable()); // When GetSupplierDescriptions is called var supplierService = new SuppliersService(mockedUnitOfWork.Object); var suppliers = supplierService.GetSupplierDescriptions(); // Then all 100 SupplierDescriptions are returned Assert.IsTrue(suppliers.Count() == 100); }
public void GetProducts_Returns_ProductDescriptions() { var entriesPerPage = 5; var page = 2; // given there are products with suppliers and categories var supplier = new Supplier { SupplierID = 1, CompanyName = "Supplier 1" }; var category = new Category { CategoryID = 1, CategoryName = "Category 1" }; var products = new List<Product>(); for (var i = 0; i < 100; i++) { var product = new Product { ProductID = i, ProductName = $"Product {i}", Category = category, Supplier = supplier }; products.Add(product); } var mockedUnitOfWork = new Mock<IUnitOfWork>(); var mockedProductRepository = new Mock<IRepository<Product>>(); mockedUnitOfWork.Setup(e => e.GetRepository<Product>()).Returns(mockedProductRepository.Object); mockedProductRepository.Setup(e => e.GetAll()).Returns(products.AsQueryable()); // when GetProducts is called var productsService = new ProductsService(mockedUnitOfWork.Object); var productDescriptions = productsService.GetProducts(entriesPerPage, page).Data; // then correct set of ProductDescriptions are returned Assert.IsTrue(productDescriptions.Count() == 5, "Descriptions for 5 products are returned"); Assert.IsTrue(productDescriptions.Select(e => e.Id).Intersect(new [] { 5, 6, 7, 8, 9 }).Count() == 5, "Products in page 2 are returned"); }