Exemplo n.º 1
0
        public async Task GetByCategoryIdShouldReturnOnlyTheCategory()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;

            AutoMapperConfig.RegisterMappings(Assembly.Load("CookingBook.Web.ViewModels"));
            var dbContext = new ApplicationDbContext(options);

            var category  = new Category();
            var nutrValue = new NutritionValue();
            var user      = new ApplicationUser();
            var prod      = new Collection <RecipeByIdProductsViewModel>();

            dbContext.Recipes.Add(new Recipe
            {
                Id         = "a", CreatedOn = DateTime.Parse("2008-11-01T19:31:00.0000000Z"), Title = "newTitle1",
                CategoryId = 1,
            });
            dbContext.Recipes.Add(new Recipe
            {
                Id         = "b", CreatedOn = DateTime.Parse("2008-11-01T19:32:00.0000000Z"), Title = "newTitle2",
                CategoryId = 1,
            });
            dbContext.Recipes.Add(new Recipe
            {
                Id         = "c", CreatedOn = DateTime.Parse("2008-11-01T19:33:00.0000000Z"), Title = "newTitle3",
                CategoryId = 2,
            });
            dbContext.Recipes.Add(new Recipe
            {
                Id         = "d", CreatedOn = DateTime.Parse("2008-11-01T19:34:00.0000000Z"), Title = "newTitle4",
                CategoryId = 3,
            });
            dbContext.Recipes.Add(new Recipe
            {
                Id         = "e", CreatedOn = DateTime.Parse("2008-11-01T19:35:00.0000000Z"), Title = "newTitle5",
                CategoryId = 4,
            });
            dbContext.Recipes.Add(new Recipe
            {
                Id         = "f", CreatedOn = DateTime.Parse("2008-11-01T19:36:00.0000000Z"), Title = "newTitle6",
                CategoryId = 4,
            });
            dbContext.Recipes.Add(new Recipe
            {
                Id         = "g", CreatedOn = DateTime.Parse("2008-11-01T19:37:00.0000000Z"), Title = "newTitle7",
                CategoryId = 4,
            });
            dbContext.Recipes.Add(new Recipe
            {
                Id         = "h", CreatedOn = DateTime.Parse("2008-11-01T19:38:00.0000000Z"), Title = "newTitle8",
                CategoryId = 4,
            });
            await dbContext.SaveChangesAsync();

            var recipeRepo    = new EfDeletableEntityRepository <Recipe>(dbContext);
            var nutritionRepo = new EfDeletableEntityRepository <NutritionValue>(dbContext);
            var productRepo   = new EfDeletableEntityRepository <Product>(dbContext);
            var userRepo      = new EfDeletableEntityRepository <ApplicationUser>(dbContext);
            var service       = new RecipesService(recipeRepo, nutritionRepo, productRepo, userRepo);

            Assert.Equal(4, service.GetByCategoryId <RecipeDTO>(4).Count());
        }