示例#1
0
        public async Task DeleteByIdAsyncShouldDeleteTheProperEntity()
        {
            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);

            var result = service.DeleteById(3);

            Assert.DoesNotContain(new Category {
                Id = 3, Title = "thirdCategory", Image = "thirdImage"
            }, dbContext.Categories);
            await Assert.ThrowsAnyAsync <Exception>(() => service.DeleteById(234));
        }
        public void Throw_WhenIdParameterIsInvalid()
        {
            //Arrange
            var categories        = new Mock <IEfGenericRepository <Category> >();
            var inavlidCategoryId = 0;
            var categoriesService = new CategoriesService(categories.Object);

            //Act & Assert
            Assert.Throws <ArgumentOutOfRangeException>(() => categoriesService.DeleteById(inavlidCategoryId));
        }
        public void InvokeRepositoryMethosDeleteOnce()
        {
            //Arrange
            var categories = new Mock <IEfGenericRepository <Category> >();
            var category   = DataHelper.GetCategory();
            var categoryId = category.Id;

            categories.Setup(x => x.Delete(It.IsAny <int>())).Verifiable();
            var categoriesService = new CategoriesService(categories.Object);

            //Act
            categoriesService.DeleteById(categoryId);

            //Assert
            categories.Verify(x => x.Delete(It.IsAny <int>()), Times.Once);
        }