2013-01-05 79 views
13

我確實需要來測試CreateMap方法中的自定義邏輯。我做不是想要測試映射是否存在對於某些類型。單元測試Automapper配置文件

我該怎麼做,或者我需要知道哪些類。我很感激每一個提示文件。 automapper單元測試似乎非常罕見......

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))); 




    } 
} 

回答

10

這是配置測試的文檔:https://github.com/AutoMapper/AutoMapper/wiki/Configuration-validation

這裏你可以看到它的一個例子:https://stackoverflow.com/a/14150006/1505426

這是你所追求的?

+0

沒有看到我更新的問題與代碼。 – Pascal

+0

我錯過了一些東西。爲什麼不能編寫一個單元測試來創建'Unit'的實例,將它映射到UnitTreeViewModel,並聲明映射的對象是你期望的? – Mightymuke

+2

這是一個很好的問題。我猜這是因爲我的身邊幾乎是午夜;-) – Pascal