This simple AutoMapper
test works for me:
Classes
public class Client
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
AutoMapperConfiguration
public class MyProfile : Profile
{
protected override void Configure()
{
CreateMap<Client, Client>()
.ForAllMembers(opt => opt.Condition(src => !src.IsSourceValueNull));
}
}
Unit Tests
[TestFixture]
public class MappingTests
{
[Test]
public void AutoMapper_Configuration_IsValid()
{
Mapper.Initialize(m => m.AddProfile<MyProfile>());
Mapper.AssertConfigurationIsValid();
}
[Test]
public void AutoMapper_ClientMapping_IsValid()
{
Mapper.Initialize(m => m.AddProfile<MyProfile>());
Mapper.AssertConfigurationIsValid();
var source = new Client
{
FirstName = "SourceFirstName1",
LastName = null
};
var destination = new Client
{
FirstName = "DestinationFirstName1",
LastName = "DestinationLastName1"
};
destination = Mapper.Map(source, destination);
Assert.That(destination, Is.Not.Null);
Assert.That(destination.FirstName, Is.EqualTo("SourceFirstName1"));
Assert.That(destination.LastName, Is.EqualTo("DestinationLastName1"));
}
}
UPDATE
Interestingly, when you use this mapping to map a list, it fails. IE - this test fails:
[Test]
public void AutoMapper_ClientListMapping_IsValid()
{
Mapper.Initialize(m => m.AddProfile<MyProfile>());
Mapper.AssertConfigurationIsValid();
var source = new List<Client>
{
new Client
{
FirstName = "SourceFirstName1",
LastName = null
},
new Client
{
FirstName = null,
LastName = "SourceLastName2"
}
};
var destination = new List<Client>
{
new Client
{
FirstName = "DestinationFirstName1",
LastName = "DestinationLastName1"
},
new Client
{
FirstName = "DestinationFirstName2",
LastName = "DestinationLastName2"
}
};
destination = Mapper.Map(source, destination);
Assert.That(destination, Is.Not.Null);
Assert.That(destination.Count, Is.EqualTo(2));
Assert.That(destination[0].FirstName, Is.EqualTo("SourceFirstName1"));
Assert.That(destination[0].LastName, Is.EqualTo("DestinationLastName1"));
// /\ Line above went BANG! /\
Assert.That(destination[1].FirstName, Is.EqualTo("DestinationFirstName2"));
Assert.That(destination[1].LastName, Is.EqualTo("SourceLastName2"));
}
This looks like a bug in AutoMapper (in 2.2.0 and 2.2.1-ci9000)