// GET api/<controller> public IHttpActionResult Get() { IHttpActionResult result = null; SubCategoryService service = new SubCategoryService(); IEnumerable <SubCategory> subCategories = service.GetSubCategories(); if (subCategories.Count() > 0) { result = Ok(subCategories); } else { result = NotFound(); } return(result); }
public async Task GetSubCategories_WithNoSubCategories_ShouldReturnAnEmptyCollection() { string onCountDifferenceErrorMessage = "The method did not return an empty collection."; var context = ApplicationDbContextInMemoryFactory.InitializeContext(); var subCategoryService = new SubCategoryService(context); var methodResult = await subCategoryService.GetSubCategories(); var expectedCount = 0; AssertExtensions.EqualCountWithMessage( expectedCount, methodResult.Count(), onCountDifferenceErrorMessage); }
public async Task GetSubCategories_WithSeededSubCategories_ShouldReturnCorrectResult() { string onFalseErrorMessage = "The returned sub-categories are not correct."; var context = ApplicationDbContextInMemoryFactory.InitializeContext(); var subCategoryService = new SubCategoryService(context); // Seeding multiple categories with sub-categories var expectedSubCategories = await this.SeedAndGetSubCategoriesWithCategories(context); var actualSubCategories = await subCategoryService.GetSubCategories(); // The returned names contains the following format -> "SubCategoryName(MainCategoryName)" foreach (var ctg in actualSubCategories) { Assert.True( expectedSubCategories.Any(c => c.Name + $"({c.Category.Name})" == ctg.Text && c.Id == ctg.Value), onFalseErrorMessage); } }