Pas*_*cal 20 unit-testing automapper
我想测试方法中的自定义逻辑CreateMap.我不是要测试的映射是否都存在某些类型.
我该怎么做或者我需要知道的课程是什么.我很感激每一个提示文件.自动映射单元测试似乎非常罕见......
public class UnitProfile : Profile
{
protected override void Configure()
{
// Here I create my maps with custom logic that needs to be tested
CreateMap<Unit, UnitTreeViewModel>()
.ForMember(dest => dest.IsFolder, o => o.MapFrom(src => src.UnitTypeState == UnitType.Folder ? true : false));
CreateMap<CreateUnitViewModel, Unit>()
.ForMember(dest => dest.UnitTypeState, o => o.MapFrom(src => (UnitType)Enum.ToObject(typeof(UnitType), src.SelectedFolderTypeId)));
}
}
Run Code Online (Sandbox Code Playgroud)
Mig*_*uke 24
这是配置测试的文档:http://docs.automapper.org/en/stable/Configuration-validation.html
您可以在此处查看示例:https://stackoverflow.com/a/14150006/1505426
这是你追求的吗?
Krz*_*tof 12
Automapper 配置文件的测试示例(我使用了 Automapper 版本10.0.0和 NUnit 版本3.12.0):
行状态枚举
namespace StackOverflow.RowStatus
{
public enum RowStatusEnum
{
Modified = 1,
Removed = 2,
Added = 3
}
}
Run Code Online (Sandbox Code Playgroud)
我的简历
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
映射测试
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)