Automapper 配置文件的测试示例(我在 version 中使用 Automapper,在 version中使用10.0.0
NUnit 3.12.0
):
行状态枚举
namespace StackOverflow.RowStatus
{
public enum RowStatusEnum
{
Modified = 1,
Removed = 2,
Added = 3
}
}
我的简历
namespace StackOverflow.RowStatus
{
using System;
using System.Linq;
using AutoMapper;
public class MyProfile : Profile
{
public MyProfile()
{
CreateMap<byte, RowStatusEnum>().ConvertUsing(
x => Enum.GetValues(typeof(RowStatusEnum))
.Cast<RowStatusEnum>().First(y => (byte)y == x));
CreateMap<RowStatusEnum, byte>().ConvertUsing(
x => (byte)x);
}
}
}
映射测试
namespace StackOverflow.RowStatus
{
using AutoMapper;
using NUnit.Framework;
[TestFixture]
public class MappingTests
{
[Test]
public void AutoMapper_Configuration_IsValid()
{
var config = new MapperConfiguration(cfg => cfg.AddProfile<MyProfile>());
config.AssertConfigurationIsValid();
}
[TestCase(1, ExpectedResult = RowStatusEnum.Modified)]
[TestCase(2, ExpectedResult = RowStatusEnum.Removed)]
[TestCase(3, ExpectedResult = RowStatusEnum.Added)]
public RowStatusEnum AutoMapper_ConvertFromByte_IsValid(
byte rowStatusEnum)
{
var config = new MapperConfiguration(cfg => cfg.AddProfile<MyProfile>());
var mapper = config.CreateMapper();
return mapper.Map<byte, RowStatusEnum>(rowStatusEnum);
}
[TestCase(RowStatusEnum.Modified, ExpectedResult = 1)]
[TestCase(RowStatusEnum.Removed, ExpectedResult = 2)]
[TestCase(RowStatusEnum.Added, ExpectedResult = 3)]
public byte AutoMapper_ConvertFromEnum_IsValid(
RowStatusEnum rowStatusEnum)
{
var config = new MapperConfiguration(cfg => cfg.AddProfile<MyProfile>());
var mapper = config.CreateMapper();
return mapper.Map<RowStatusEnum, byte>(rowStatusEnum);
}
}
}