116 lines
3.4 KiB
C#
116 lines
3.4 KiB
C#
using AutoMapper;
|
|
using GoodWood.Application.DTOs;
|
|
using GoodWood.Domain.Entities;
|
|
using GoodWood.Domain.Interfaces;
|
|
using Moq;
|
|
|
|
namespace GoodWood.Tests.Common;
|
|
|
|
/// <summary>
|
|
/// Extensions for setting up common mocks.
|
|
/// </summary>
|
|
public static class TestMockHelpers
|
|
{
|
|
/// <summary>
|
|
/// Sets up IClubRepository mock with standard behavior.
|
|
/// </summary>
|
|
public static Mock<IClubRepository> SetupGetAllAsync(
|
|
this Mock<IClubRepository> mock,
|
|
List<Club> clubs)
|
|
{
|
|
mock.Setup(r => r.GetAllAsync(It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(clubs);
|
|
return mock;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets up IClubRepository mock GetByIdAsync.
|
|
/// </summary>
|
|
public static Mock<IClubRepository> SetupGetByIdAsync(
|
|
this Mock<IClubRepository> mock,
|
|
Club? club)
|
|
{
|
|
mock.Setup(r => r.GetByIdAsync(It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(club);
|
|
return mock;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets up IClubRepository mock GetByIdAsync for specific ID.
|
|
/// </summary>
|
|
public static Mock<IClubRepository> SetupGetByIdAsync(
|
|
this Mock<IClubRepository> mock,
|
|
Guid id,
|
|
Club? club)
|
|
{
|
|
mock.Setup(r => r.GetByIdAsync(id, It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(club);
|
|
return mock;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets up IClubRepository mock AddAsync.
|
|
/// </summary>
|
|
public static Mock<IClubRepository> SetupAddAsync(
|
|
this Mock<IClubRepository> mock,
|
|
Func<Club, Club>? transform = null)
|
|
{
|
|
mock.Setup(r => r.AddAsync(It.IsAny<Club>(), It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync((Club c, CancellationToken _) => transform?.Invoke(c) ?? c);
|
|
return mock;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets up IClubRepository mock UpdateAsync.
|
|
/// </summary>
|
|
public static Mock<IClubRepository> SetupUpdateAsync(
|
|
this Mock<IClubRepository> mock)
|
|
{
|
|
mock.Setup(r => r.UpdateAsync(It.IsAny<Club>(), It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync((Club c, CancellationToken _) => c);
|
|
return mock;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets up IClubRepository mock DeleteAsync.
|
|
/// </summary>
|
|
public static Mock<IClubRepository> SetupDeleteAsync(
|
|
this Mock<IClubRepository> mock,
|
|
bool result = true)
|
|
{
|
|
mock.Setup(r => r.DeleteAsync(It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(result);
|
|
return mock;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a simple mapper mock that maps Club to ClubDto.
|
|
/// </summary>
|
|
public static Mock<IMapper> CreateClubMapperMock()
|
|
{
|
|
var mock = new Mock<IMapper>();
|
|
|
|
mock.Setup(m => m.Map<ClubDto>(It.IsAny<Club>()))
|
|
.Returns((Club c) => new ClubDto
|
|
{
|
|
Id = c.Id,
|
|
Name = c.Name,
|
|
ExpenseCalculation = c.ExpenseCalculation,
|
|
CreatedAt = c.CreatedAt,
|
|
ModifiedAt = c.ModifiedAt
|
|
});
|
|
|
|
mock.Setup(m => m.Map<List<ClubDto>>(It.IsAny<List<Club>>()))
|
|
.Returns((List<Club> clubs) => clubs.Select(c => new ClubDto
|
|
{
|
|
Id = c.Id,
|
|
Name = c.Name,
|
|
ExpenseCalculation = c.ExpenseCalculation,
|
|
CreatedAt = c.CreatedAt,
|
|
ModifiedAt = c.ModifiedAt
|
|
}).ToList());
|
|
|
|
return mock;
|
|
}
|
|
}
|