Automapper - AssertConfigurationIsValid call fails

2019-04-10 19:11发布

问题:

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

回答1:

Configuration validation is about making sure that you don't mispell something in your destination type. Since AutoMapper is inferring what you're trying to map, the test is about validating that assertion. The map can still work of course, but you might assume a destination property is going to be mapped when in reality there is no matching member.

The MemberList enum is about what list of members to validate. By default it's the destination type but in some cases we actually want to use the source type as the list of members to check against.



标签: c# AutoMapper