Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit test the Automapper profiles

I do want to test the custom logic in the CreateMap method. I do NOT want to test whether the mapping exist at all for some types.

How can I do that or what are the classes that I need to know. I am grateful for every hint The document about. Automapper unit testing seems very rare...

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)));    } } 
like image 906
Pascal Avatar asked Jan 05 '13 23:01

Pascal


People also ask

What is an AutoMapper profile?

automapper Profiles Basic Profile Profiles permit the programmer to organize maps into classes, enhancing code readability and maintainability. Any number of profiles can be created, and added to one or more configurations as needed. Profiles can be used with both the static and instance-based APIs.

What is AutoMapper good for?

AutoMapper is a library that helps with mapping objects in . NET. The manual process of mapping is tedious. Instead, with AutoMapper we can automatically map between our objects.

What is AutoMapper in Entity Framework?

AutoMapper is an object-object mapper that allows you to solve the problem of manually mapping each property of a class with the same properties of another class. Before AutoMapper was introduced if we wanted to assign one object property to another object property then we were following a long procedure.

Is AutoMapper a singleton?

Your configuration (e.g. Automapper Profiles) are singletons. That is, they are only ever loaded once when your project runs.


2 Answers

This is the documentation for configuration testing: http://docs.automapper.org/en/stable/Configuration-validation.html

You can see an example of it here: https://stackoverflow.com/a/14150006/1505426

like image 58
Mightymuke Avatar answered Oct 07 '22 12:10

Mightymuke


An example of tests for Automapper profile (I used Automapper in version 10.0.0 amd NUnit in version 3.12.0):

RowStatusEnum

namespace StackOverflow.RowStatus {     public enum RowStatusEnum     {         Modified = 1,         Removed = 2,         Added = 3     } } 

MyProfile

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

MappingTests

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);         }     } } 
like image 35
Krzysztof Madej Avatar answered Oct 07 '22 13:10

Krzysztof Madej