public async Task GetCategoryProductsAsync_IdNotPresent_RequestedResourceNotFoundExceptionThrown() { var mockUserContext = new Mock <IUserContext>(); var mockProductContext = new Mock <IProductCatalogueContext>(); List <CatalogueProduct> productList = new List <CatalogueProduct>() { new CatalogueProduct() { Id = 1, CategoryId = 1 }, new CatalogueProduct() { Id = 2, CategoryId = 2 } }; List <DbCategory> categoryList = new List <DbCategory>() { new DbCategory() { Id = 1 }, new DbCategory() { Id = 2 } }; mockProductContext.Setup(c => c.Products).ReturnsEntitySet(productList); mockProductContext.Setup(c => c.Categories).ReturnsEntitySet(categoryList); var service = new ProductCatalogueService(mockProductContext.Object, mockUserContext.Object); Assert.ThrowsAsync <RequestedResourceNotFoundException>(() => service.GetCategoryProductsAsync(0)); }
public async Task GetCategoryProductsAsync_IdPresent_ReturnsAppropriateProducts() { var mockUserContext = new Mock <IUserContext>(); var mockProductContext = new Mock <IProductCatalogueContext>(); List <CatalogueProduct> productList = new List <CatalogueProduct>() { new CatalogueProduct() { Id = 1, CategoryId = 1 }, new CatalogueProduct() { Id = 2, CategoryId = 2 } }; List <DbCategory> categoryList = new List <DbCategory>() { new DbCategory() { Id = 1 }, new DbCategory() { Id = 2 } }; mockProductContext.Setup(c => c.Products).ReturnsEntitySet(productList); mockProductContext.Setup(c => c.Categories).ReturnsEntitySet(categoryList); var service = new ProductCatalogueService(mockProductContext.Object, mockUserContext.Object); var list = await service.GetCategoryProductsAsync(1); Assert.Single(list); }
public async Task GetCategoryProductsAsync_ProductCategoryId_ListProductCategoryProductListItems( [Frozen] Mock <IProductCatalogueContext> context, ProductCatalogueService service, IFixture fixture) { // arrange var dbCategorys = fixture.CreateMany <DbProductCategory>(1).ToList(); var productCategoryId = dbCategorys[0].Id; var dbProducts = fixture.CreateMany <DbProduct>(5).OrderBy(s => s.Id).ToList(); dbProducts[1].CategoryId = productCategoryId; dbProducts[3].CategoryId = productCategoryId; dbProducts[4].CategoryId = productCategoryId; context.Setup(s => s.Categories).ReturnsEntitySet(dbCategorys); context.Setup(s => s.Products).ReturnsEntitySet(dbProducts); // act var productCategoryProductListItems = await service.GetCategoryProductsAsync(productCategoryId); // assert Assert.Equal(3, productCategoryProductListItems.Count); }
public async Task GetCategoryProductsAsync_ProductCategoryId_RequestedResourceNotFoundException( [Frozen] Mock <IProductCatalogueContext> context, ProductCatalogueService service, IFixture fixture) { // arrange var dbCategorys = fixture.CreateMany <DbProductCategory>(1).ToList(); var productCategoryId = dbCategorys[0].Id + 1; context.Setup(s => s.Categories).ReturnsEntitySet(dbCategorys); // assert await Assert.ThrowsAsync <RequestedResourceNotFoundException>(() => service.GetCategoryProductsAsync(productCategoryId)); }