using audit.Model; using audit_logs.Repositories.Interfaces; using audit_logs.Services; namespace audit_test.Service; using System; using System.Collections.Generic; using System.Threading.Tasks; using Moq; using Xunit; public class AuditServiceTests { private readonly Mock _mockRepository; private readonly AuditService _auditService; private readonly AuditRecord _testAuditRecord; public AuditServiceTests() { // creo el mock del repositorio _mockRepository = new Mock(); // preparo el servicio con el mock de repositorio _auditService = new AuditService(_mockRepository.Object); // configuracion del registro _testAuditRecord = new AuditRecord { EntityType = "User", RecordId = "1", UserId = "123", ActionType = "Create", ActionTime = DateTime.UtcNow, Changes = new List { new AuditRecord.Change { Field = "Username", OldValue = "oldUser", NewValue = "newUser", ValueType = "String" } }, Reason = "Ingreso de paciente", UserIpAddress = "127.0.0.1" }; } [Fact] public async Task RecordAuditAsync_ValidRecord_InsertsRecord() { // Arrange _mockRepository.Setup(repo => repo.InsertOneAsync(It.IsAny())) .Returns(Task.CompletedTask); // Act await _auditService.RecordAuditAsync(_testAuditRecord); // Assert _mockRepository.Verify(repo => repo.InsertOneAsync(It.IsAny()), Times.Once); } [Fact] public async Task RecordAuditAsync_NullRecord_ThrowsArgumentNullException() { // Act & Assert await Assert.ThrowsAsync(() => _auditService.RecordAuditAsync(null)); } }