Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Moq" Version="4.20.0" />
<PackageReference Include="FluentAssertions" Version="6.11.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="10.0.3" />
<PackageReference Include="xunit" Version="2.6.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.6.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Evand.Application\Evand.Application.csproj" />
<ProjectReference Include="..\..\Evand.Domain\Evand.Domain.csproj" />
<ProjectReference Include="..\..\Evand.Persistence\Evand.Persistence.csproj" />
</ItemGroup>

</Project>
107 changes: 107 additions & 0 deletions Evand-Backend/tests/Evand.Application.Tests/EventServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using System;
using System.Threading.Tasks;
using Evand.Application.DTOs.Event;
using Evand.Application.Services;
using Evand.Domain.Entities;
using Evand.Persistence.Interfaces;
using FluentAssertions;
using Moq;
using Xunit;

namespace Evand.Application.Tests
{
public class EventServiceTests
{
private readonly Mock<IGenericQueryRepository<Event>> _queryRepoMock;
private readonly Mock<IGenericCommandRepository<Event>> _commandRepoMock;
private readonly Mock<IUnitOfWork> _uowMock;
private readonly EventService _service;

public EventServiceTests()
{
_queryRepoMock = new Mock<IGenericQueryRepository<Event>>();
_commandRepoMock = new Mock<IGenericCommandRepository<Event>>();
_uowMock = new Mock<IUnitOfWork>();

_service = new EventService(
_queryRepoMock.Object,
_commandRepoMock.Object,
_uowMock.Object
);
}

[Fact]
public async Task AddAsync_Should_Call_CommandRepo_And_SaveChanges_And_Return_Entity()
{
// Arrange
var dto = new EventAddOrUpdateDto
{
Guid = Guid.NewGuid(),
Name = "UnitTest Event",
Category = "Test",
Address = "Test Address",
StartDate = DateTime.UtcNow,
EndDate = DateTime.UtcNow.AddHours(2),
Capacity = 100,
Price = 9.99m
};

var createdEntity = new Event
{
Guid = dto.Guid,
Name = dto.Name,
Category = dto.Category,
Address = dto.Address,
StartDate = dto.StartDate,
EndDate = dto.EndDate,
Capacity = dto.Capacity,
Price = dto.Price
};

_commandRepoMock
.Setup(r => r.AddAsync(It.IsAny<Event>()))
.ReturnsAsync(createdEntity);

_uowMock
.Setup(u => u.SaveChangesAsync(default))
.ReturnsAsync(1);

// Act
var result = await _service.AddAsync(dto);

// Assert
result.Should().NotBeNull("service should return created entity");
result.Should().BeEquivalentTo(createdEntity, options => options
.Excluding(e => e.Id));

_commandRepoMock.Verify(r => r.AddAsync(It.Is<Event>(e => e.Name == dto.Name && e.Guid == dto.Guid)), Times.Once);
_uowMock.Verify(u => u.SaveChangesAsync(default), Times.Once);
}

[Fact]
public async Task GetAllAsync_Should_Return_List_Of_Events()
{
// Arrange: Mocking data
var events = new[]
{
new Event { Guid = Guid.NewGuid(), Name = "Event 1", Category = "Test", Address = "Address 1" },
new Event { Guid = Guid.NewGuid(), Name = "Event 2", Category = "Test", Address = "Address 2" }
};

_queryRepoMock
.Setup(r => r.GetAllAsync())
.ReturnsAsync(events);

// Act: calling service method
var result = await _service.GetAllAsync();
// Assert:check result
result.Should().NotBeNull("service should return a list of events");
result.Should().HaveCount(2, "we have set up 2 events in the mock");
result.Should().BeEquivalentTo(events, options => options
.Excluding(e => e.Id));

_queryRepoMock.Verify(r => r.GetAllAsync(), Times.Once);
}

}
}