Ploeh AutoFixture was unable to create an instance from System.Runtime.Serialization.ExtensionDataObject

前端 未结 3 1167
遥遥无期
遥遥无期 2021-01-17 07:44

We have an MVC project with references to WCF services. Those references added (ExtensionDataObject)ExtensionData property to every DTO and Response object and

相关标签:
3条回答
  • 2021-01-17 08:01

    The easiest way to do it is:

    fixture.Register<ExtensionDataObject>(() => null);
    

    That registers to AutoFixture a specific way to initialize all the ExtensionDataObject, with the Func given. In this case the Func always returns null so you are good to go.

    0 讨论(0)
  • 2021-01-17 08:07

    To make it bit DRYer and CTRL+C friendly, here is Spiros Dellaportases (thanks!) answer wrapped in fixture Customization:

    public class OmitExtensionDataObjectPropertyCustomization : ICustomization
    {
        public void Customize(IFixture fixture)
        {
            fixture.Register<ExtensionDataObject>(() => null);
        }
    }
    
    0 讨论(0)
  • 2021-01-17 08:16

    I hope someone will find it useful, I've managed to get it to work with the PropertyTypeOmitter class as per the this thread:

    public void Test()
    {
        var fixture = new Fixture();
        fixture.Customizations.Add(
            new PropertyTypeOmitter(
                typeof(ExtensionDataObject)));
    
        var person = fixture.CreateAnonymous<Person>();
    }
    
    internal class PropertyTypeOmitter : ISpecimenBuilder
    {
        private readonly Type type;
    
        internal PropertyTypeOmitter(Type type)
        {
            if (type == null)
                throw new ArgumentNullException("type");
    
            this.type = type;
        }
    
        internal Type Type
        {
            get { return this.type; }
        }
    
        public object Create(object request, ISpecimenContext context)
        {
            var propInfo = request as PropertyInfo;
            if (propInfo != null && propInfo.PropertyType == type)
                return new OmitSpecimen();
    
            return new NoSpecimen();
        }
    }
    
    0 讨论(0)
提交回复
热议问题