Inicio
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
using audit.Model;
|
||||
using audit.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<IAuditRecordRepository> _mockRepository;
|
||||
private readonly AuditService _auditService;
|
||||
private readonly AuditRecord _testAuditRecord;
|
||||
|
||||
public AuditServiceTests()
|
||||
{
|
||||
// creo el mock del repositorio
|
||||
_mockRepository = new Mock<IAuditRecordRepository>();
|
||||
|
||||
// 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<AuditRecord.Change>
|
||||
{
|
||||
new AuditRecord.Change { Field = "Username", OldValue = "oldUser", NewValue = "newUser", ValueType = "String" }
|
||||
},
|
||||
Reason = "Ingreso de paciente"
|
||||
};
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task RecordAuditAsync_ValidRecord_InsertsRecord()
|
||||
{
|
||||
// Arrange
|
||||
_mockRepository.Setup(repo => repo.InsertOneAsync(It.IsAny<AuditRecord>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
await _auditService.RecordAuditAsync(
|
||||
_testAuditRecord.EntityType,
|
||||
_testAuditRecord.RecordId,
|
||||
_testAuditRecord.UserId,
|
||||
_testAuditRecord.ActionType,
|
||||
_testAuditRecord.Reason,
|
||||
_testAuditRecord.ActionTime,
|
||||
_testAuditRecord.Changes
|
||||
);
|
||||
|
||||
// Assert
|
||||
_mockRepository.Verify(repo => repo.InsertOneAsync(It.IsAny<AuditRecord>()), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task RecordAuditAsync_NullRecord_ThrowsArgumentNullException()
|
||||
{
|
||||
// Act & Assert
|
||||
await Assert.ThrowsAsync<ArgumentNullException>(() =>
|
||||
_auditService.RecordAuditAsync(null, null, null, null, null,DateTime.MinValue, null));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user