MinimalAPIAsp/tests/Application.UnitTests/Common/Mappings/MappingTests.cs

54 lines
1.6 KiB
C#

using System.Reflection;
using System.Runtime.CompilerServices;
using AutoMapper;
using MiniSkeletonAPI.Application.Common.Interfaces;
using MiniSkeletonAPI.Application.Common.Models;
using MiniSkeletonAPI.Application.TodoItems.Queries.GetTodoItemsWithPagination;
using MiniSkeletonAPI.Application.TodoLists.Queries.GetTodos;
using MiniSkeletonAPI.Domain.Entities;
using NUnit.Framework;
namespace Application.UnitTests.Common.Mappings;
public class MappingTests
{
private readonly IConfigurationProvider _configuration;
private readonly IMapper _mapper;
public MappingTests()
{
_configuration = new MapperConfiguration(config =>
config.AddMaps(Assembly.GetAssembly(typeof(IApplicationDbContext))));
_mapper = _configuration.CreateMapper();
}
[Test]
public void ShouldHaveValidConfiguration()
{
_configuration.AssertConfigurationIsValid();
}
[Test]
[TestCase(typeof(TodoList), typeof(TodoListDto))]
[TestCase(typeof(TodoItem), typeof(TodoItemDto))]
[TestCase(typeof(TodoList), typeof(LookupDto))]
[TestCase(typeof(TodoItem), typeof(LookupDto))]
[TestCase(typeof(TodoItem), typeof(TodoItemBriefDto))]
public void ShouldSupportMappingFromSourceToDestination(Type source, Type destination)
{
var instance = GetInstanceOf(source);
_mapper.Map(instance, source, destination);
}
private object GetInstanceOf(Type type)
{
if (type.GetConstructor(Type.EmptyTypes) != null)
return Activator.CreateInstance(type)!;
// Type without parameterless constructor
return RuntimeHelpers.GetUninitializedObject(type);
}
}