public async Task DeleteMethodShouldChangeIsDeletedAndDeletedOn(string name) { var options = new DbContextOptionsBuilder <ForumDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var db = new ForumDbContext(options); var dateTimeProvider = new Mock <IDateTimeProvider>(); dateTimeProvider.Setup(dtp => dtp.Now()).Returns(new DateTime(2020, 3, 27)); await db.Categories.AddAsync(new Category { Name = name, CreatedOn = dateTimeProvider.Object.Now() }); await db.SaveChangesAsync(); var categoriesService = new CategoriesService(db, null, dateTimeProvider.Object); await categoriesService.DeleteAsync(1); var actual = await db.Categories.FirstOrDefaultAsync(); actual.IsDeleted.Should().BeTrue(); actual.DeletedOn.Should().BeSameDateAs(dateTimeProvider.Object.Now()); }
public void DeleteAsync_Calls_Mediator() { //Arrange //Act _categoriesService.DeleteAsync(1); //Assert _mockMediator.Verify(x => x.Send(It.IsAny <DeleteCategoryCommand>(), default(CancellationToken)), Times.Once()); }
public async Task DeleteAsyncCategoryNotValid() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var repository = new EfDeletableEntityRepository <Category>(new ApplicationDbContext(options.Options)); var categoryService = new CategoriesService(repository); AutoMapperConfig.RegisterMappings(typeof(MyTestPost).Assembly); repository.AddAsync(new Category() { Title = "test" }).GetAwaiter().GetResult(); repository.SaveChangesAsync().GetAwaiter().GetResult(); await Assert.ThrowsAsync <Exception>(() => categoryService.DeleteAsync(18)); }
public async Task DeleteAsyncWorksCorrectly() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options; var dbContext = new ApplicationDbContext(options); var repository = new EfDeletableEntityRepository <Category>(dbContext); var service = new CategoriesService(repository); var category = new Category() { Id = 1, }; var childCategoryOne = new Category() { Id = 2, ParentCategoryId = 1, }; var childCategoryTwo = new Category() { Id = 3, ParentCategoryId = 1, }; dbContext.Add(category); dbContext.Add(childCategoryOne); dbContext.Add(childCategoryTwo); await dbContext.SaveChangesAsync(); await service.DeleteAsync(1); var categoriesInDbCount = dbContext.Categories.ToList().Count(); Assert.Equal(0, categoriesInDbCount); }