public AuditServiceTestsFixture() { var autoFixture = new Fixture(); UserId = autoFixture.Create <string>(); UserName = autoFixture.Create <string>(); UserAction = autoFixture.Create <UserAction>(); TestUpdateAuditEntity = new TestAuditEntity(autoFixture.Create <Guid>()); TestInsertAuditEntity = new TestAuditEntity(autoFixture.Create <Guid>()); TestDeleteAuditEntity = new TestAuditEntity(autoFixture.Create <Guid>()); TestUpdateInitialState = autoFixture.Create <Dictionary <string, object> >(); TestDeleteInitialState = autoFixture.Create <Dictionary <string, object> >(); TestInsertUpdatedState = autoFixture.Create <Dictionary <string, object> >(); TestUpdateUpdatedState = autoFixture.Create <Dictionary <string, object> >(); StateService = new Mock <IStateService>(); StateService.Setup(x => x.GetState(It.Is <object>(o => o == TestUpdateAuditEntity))).Returns(TestUpdateInitialState); StateService.Setup(x => x.GetState(It.Is <object>(o => o == TestDeleteAuditEntity))).Returns(TestDeleteInitialState); Repository = new Mock <IAuditRepository>(); Repository.Setup(x => x.Add(It.IsAny <Audit>())); AuditService = new AuditService(StateService.Object, Mock.Of <IDiffService>(), Repository.Object); AuditService.StartTracking(UserAction, UserId, UserName); }
public async Task CheckAudit() { using var scope = Services; var provider = scope.ServiceProvider; var repo = provider.GetService <TestAuditEntityRepository>(); var unitOfWork = provider.GetService <IUnitOfWork>(); var entity1 = new TestAuditEntity() { Name = "name1" }; await repo.AddAsync(entity1); await unitOfWork.CommitAsync(); var entityForUpdate = await repo.GetByIdAsync(entity1.Id); Assert.NotNull(entityForUpdate.CreatedById); Assert.NotNull(entityForUpdate.Created); entityForUpdate.Name = "name2"; await repo.UpdateAsync(entityForUpdate); await unitOfWork.CommitAsync(); var updatedEntity = await repo.GetByIdAsync(entity1.Id); Assert.NotNull(updatedEntity.UpdatedById); Assert.NotNull(updatedEntity.Updated); }