Not sure if this is a bug or I'm not using it right, but seems like Automapper can map properties even though AssertConfigurationIsValid
fails. In the following tests, ShouldMapSourceList
will pass even though AssertConfigurationIsValid
fails in ShouldValidateAgainstSourceListOnly
:
using AutoMapper;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace AutoMapperTests
{
[TestClass]
public class CreateMapTests
{
private class A
{
public string PropID { get; set; }
public string PropB { get; set; }
}
private class B
{
public string PropId { get; set; }
public string PropB { get; set; }
public string PropC { get; set; }
}
internal class CreateMapTestProfile : Profile
{
protected override void Configure()
{
// will complain about Unmapped member PropC when AssertConfigurationIsValid is called.
CreateMap<A, B>();
}
}
internal class CreateMapTestWithSourceMemberListProfile : Profile
{
protected override void Configure()
{
// will complain about Unmapped member PropID when AssertConfigurationIsValid is called.
CreateMap<A, B>(MemberList.Source);
}
}
[TestMethod]
public void ShouldMapSourceList()
{
Mapper.AddProfile<CreateMapTestWithSourceMemberListProfile>();
//Mapper.AssertConfigurationIsValid();
var a = new A
{
PropID = "someId",
PropB = "random",
};
var actual = Mapper.Map<B>(a);
Assert.AreEqual("someId", actual.PropId);
}
[TestMethod]
public void ShouldValidateAgainstSourceListOnly()
{
Mapper.AddProfile<CreateMapTestWithSourceMemberListProfile>();
Mapper.AssertConfigurationIsValid();
// if we got here without exceptions, it means we're good!
Assert.IsTrue(true);
}
}
}
Shouldn't the mapping fail if configuration is not valid? Or if the configuration is valid, why does AssertConfigurationIsValid
fail?
Test project here: https://github.com/mrchief/AutoMapperTests/blob/master/CreateMapTests.cs