public async Task SaveChanges_ShouldSetAuditData_WhenUpdateEntity() { // Arrange var now = DateTime.UtcNow; _datetime.Setup(x => x.Now).Returns(now); User user = GetDefaultUser(); _currentUser.Setup(x => x.UserId).Returns(user.Id); var entity = new CustomAuditableEntity(); var sut = new CustomAppDbContext(_options, _currentUser.Object, _datetime.Object, _mediator.Object); // Act await sut.AddRangeAsync(user, entity); await sut.SaveChangesAsync(); sut.Update(entity); await sut.SaveChangesAsync(); // Assert entity.UpdatedAt.Should().Be(now); entity.UpdatedById.Should().Be(user.Id); }
public async Task SaveChanges_ShouldSupportSoftDelete() { // Arrange var entity = new CustomSoftDeleteEntity(); var sut = new CustomAppDbContext(_options, _currentUser.Object, _datetime.Object, _mediator.Object); // Act await sut.AddAsync(entity); await sut.SaveChangesAsync(); sut.Remove(entity); await sut.SaveChangesAsync(); // Assert entity.IsDeleted.Should().BeTrue(); }
public async Task SaveChanges_ShouldRaiseEvent() { // Arrange INotification eve = new CustomEvent(); User user = GetDefaultUser(); user.Events.Add(eve); var sut = new CustomAppDbContext(_options, _currentUser.Object, _datetime.Object, _mediator.Object); // Act await sut.AddAsync(user); await sut.SaveChangesAsync(); // Assert _mediator.Verify(x => x.Publish(eve, It.IsAny <CancellationToken>()), Times.Once); }