Automapper-AssertConfigurationIsValid调用失败,但映射仍然有效 [英] Automapper - AssertConfigurationIsValid call fails but mapping still works

查看:300
本文介绍了Automapper-AssertConfigurationIsValid调用失败,但映射仍然有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不确定这是错误还是我没有正确使用它,但是即使AssertConfigurationIsValid失败,似乎Automapper仍可以映射属性.在以下测试中,即使AssertConfigurationIsValidShouldValidateAgainstSourceListOnly中失败,ShouldMapSourceList也将通过:

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

如果配置无效,映射是否会失败?或者,如果配置有效,为什么AssertConfigurationIsValid失败?

Shouldn't the mapping fail if configuration is not valid? Or if the configuration is valid, why does AssertConfigurationIsValid fail?

此处测试项目: https://github.com/mrchief/AutoMapperTests/blob/master/CreateMapTests.cs

推荐答案

配置验证旨在确保您不会误拼目标类型中的某些内容.由于AutoMapper会推断您要映射的内容,因此测试将验证该断言.该地图当然仍然可以工作,但是当实际上没有匹配的成员时,您可能会假设将映射一个目标属性.

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.

MemberList枚举有关要验证的成员列表.默认情况下,它是目标类型,但在某些情况下,我们实际上想使用源类型作为要检查的成员列表.

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.

这篇关于Automapper-AssertConfigurationIsValid调用失败,但映射仍然有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆