public async Task GetProducts_ShopHistoryApiError_InternalServcerError() { var product1 = new Product() { Name = "Product 3", Price = 40 }; var products = new List <Product> { product1 }; var shopperHistories = new List <ShopperHistory> { new ShopperHistory() { CustomerId = 1, Products = new Product[] { product1 } }, }; var repository = new Mock <IDataRepository>(); repository.Setup(r => r.GetProductsAsync()).Returns(Task.FromResult <IEnumerable <Product> >(products)); repository.Setup(r => r.GetShopperHistoriesAsync()).Throws(new ApiException("Error during call to ShopperHistories API")); var shopperHistoryService = new ShopperHistoryService(repository.Object); var recommendedProductsService = new RecommendedProductsService(shopperHistoryService); var productService = new ProductService(recommendedProductsService); var controller = new ProductController(repository.Object, productService); var exception = await Assert.ThrowsAsync <ApiException>(async() => await controller.GetAsync("Recommended")); Assert.Equal("Error during call to ShopperHistories API", exception.Message); }
public ProductControllerTests() { var product1 = new Product() { Name = "Product 3", Price = 40 }; var product2 = new Product() { Name = "Product 2", Price = 60 }; var product3 = new Product() { Name = "Product 1", Price = 100 }; var products = new List <Product> { product1, product2, product3 }; var shopperHistories = new List <ShopperHistory> { new ShopperHistory() { CustomerId = 1, Products = new Product[] { product1, product2, product3 } }, new ShopperHistory() { CustomerId = 2, Products = new Product[] { product2, product3 } }, new ShopperHistory() { CustomerId = 3, Products = new Product[] { product2 } } }; var repository = new Mock <IDataRepository>(); repository.Setup(r => r.GetProductsAsync()).Returns(Task.FromResult <IEnumerable <Product> >(products)); repository.Setup(r => r.GetShopperHistoriesAsync()).Returns(Task.FromResult <IEnumerable <ShopperHistory> >(shopperHistories)); var shopperHistoryService = new ShopperHistoryService(repository.Object); var recommendedProductsService = new RecommendedProductsService(shopperHistoryService); var productService = new ProductService(recommendedProductsService); Controller = new ProductController(repository.Object, productService); }