示例#1
0
        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);
        }
示例#2
0
        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();
        }
示例#3
0
        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);
        }