Example #1
0
        public async Task EditByIdShouldChangeTheCategoryName()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;

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

            dbContext.Add(new Category {
                Id = 1, Title = "firstCategory", Image = "firstImage"
            });
            dbContext.Add(new Category {
                Id = 2, Title = "secondCategory", Image = "secondImage"
            });
            dbContext.Add(new Category {
                Id = 3, Title = "thirdCategory", Image = "thirdImage"
            });
            await dbContext.SaveChangesAsync();

            var repository = new EfDeletableEntityRepository <Category>(dbContext);
            var service    = new CategoriesService(repository);

            await service.EditById(2, "secondCategoryNew");

            Assert.Equal("secondCategoryNew", dbContext.Categories.FirstOrDefaultAsync(x => x.Id == 2).Result.Title);
            Assert.DoesNotContain(new Category {
                Id = 2, Title = "secondCategory", Image = "secondImage"
            }, dbContext.Categories);
            await Assert.ThrowsAnyAsync <Exception>(() => service.EditById(222, "asdf"));
        }