public async void GetAllProductsTest() { Mock <IProductRepository> mock = new Mock <IProductRepository>(); mock.Setup(m => m.Products).Returns(new Product[] { new Product { ID = 1, Name = "P1", Category = "C1" }, new Product { ID = 2, Name = "P2", Category = "C1" }, new Product { ID = 3, Name = "P3", Category = "C2" }, new Product { ID = 4, Name = "P4", Category = "C2" }, }.AsQueryable()); ProductsAPIController controller = new ProductsAPIController(mock.Object); var okResult = (await controller.GetProducts()) as OkObjectResult; var result = okResult.Value as IEnumerable <Product>; Assert.Equal(4, result.Count()); Assert.Equal("P1", result.ElementAt(0).Name); Assert.Equal("P2", result.ElementAt(1).Name); Assert.Equal("P3", result.ElementAt(2).Name); Assert.Equal("P4", result.ElementAt(3).Name); }
public async void GetProducts_ActionExecutes_ReturnApiOkResultWithProduct() { _mockRepo.Setup(x => x.GetAll()).ReturnsAsync(products); var resultList = await _apiController.GetProducts(); var okReturn = Assert.IsType <OkObjectResult>(resultList); var retProducts = Assert.IsAssignableFrom <IEnumerable <Product> >(okReturn.Value); Assert.Equal(2, retProducts.Count()); }