Skip to content

Instantly share code, notes, and snippets.

@anton-abyzov
Last active November 10, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anton-abyzov/39f177bfc61175ed5fbc to your computer and use it in GitHub Desktop.
Save anton-abyzov/39f177bfc61175ed5fbc to your computer and use it in GitHub Desktop.
Automapper will ignore lacking or excessed props when doing mapping
/// <summary>
/// Automapper will ignore lacking or excessed props when doing mapping
/// </summary>
public class AutoMapperTests
{
/// <summary>
/// Automapper will ignore outer props of destination entity as it has more props than the source one. Props that are not matched will be
/// populated with default values
/// </summary>
[Test]
public void OuterPropsShouldNotAbortMapping()
{
//arrange
const string helloWorld = "Hello world";
Mapper.CreateMap<SmallEntity, BigEntity>();
var smallEntity = new SmallEntity {Name = helloWorld};
//act
var result = smallEntity.Map<SmallEntity, BigEntity>();
//assert
Assert.AreEqual(result.Name, helloWorld);
Assert.AreEqual(result.ExternalEntity, null);
Assert.AreEqual(result.IntField, 0);
}
/// <summary>
/// Automapper will populate only those props that do match with source object
/// </summary>
[Test]
public void MissingPropsInDestObjectShouldNotAbortMapping()
{
//arrange
const string helloWorld = "Hello world";
Mapper.CreateMap<BigEntity, SmallEntity>();
var bigEntity = new BigEntity { Name = helloWorld, IntField = 13, ExternalEntity = new object()};
//act
var result = bigEntity.Map<BigEntity, SmallEntity>();
//assert
Assert.AreEqual(result.Name, helloWorld);
}
public class SmallEntity
{
public string Name { get; set; }
}
public class BigEntity
{
public string Name { get; set; }
public object ExternalEntity { get; set; }
public int IntField { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment