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

3

29

We have an MVC project with references to WCF services. Those references added (ExtensionDataObject)ExtensionData property to every DTO and Response object and now AutoFixture fails to create anonymous instances of these types.

Example:

public partial class SearchResultsDto : object, 
    System.Runtime.Serialization.IExtensibleDataObject, 
    System.ComponentModel.INotifyPropertyChanged {

    [System.NonSerializedAttribute()]
    private System.Runtime.Serialization.ExtensionDataObject extensionDataField;

    [global::System.ComponentModel.BrowsableAttribute(false)]
    public System.Runtime.Serialization.ExtensionDataObject ExtensionData {
        get {
                return this.extensionDataField;
            }
        set {
                this.extensionDataField = value;
            }
        }
    }

Code:

_fixture = new Fixture().Customize(new AutoMoqCustomization());
var dto = _fixture.CreateAnonymous<SearchResultsDto>();

Exception:

Ploeh.AutoFixture.ObjectCreationException: Ploeh.AutoFixture.ObjectCreationException: AutoFixture was unable to create an instance from System.Runtime.Serialization.ExtensionDataObject, most likely because it has no public constructor, is an abstract or non-public type..

Question: Is there a way of registering this object within the AutoFixture, so that it instantiates it as null or anything else, which would let me do CreateAnonymous on all objects with ExtensionData property.

Scuta answered 18/3, 2013 at 14:11 Comment(0)
P
45

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.

Pickerel answered 31/7, 2013 at 11:33 Comment(1)
This works great. Is Chuck's answer and this one do the same, eventually?Tea
S
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();
    }
}
Scuta answered 18/3, 2013 at 14:32 Comment(1)
Since AutoFixture 3.x the last line of code triggers a warning caused by the contstructor call being marked as Obsolete. In order to remove it, change it to: new NoSpecimen()Brett
T
3

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);
    }
}
Tumefy answered 25/4, 2018 at 10:41 Comment(2)
Why are you copying someone else's answer?Sacral
The explanation is in very first sentence of the answer. Should help people to keep their code DRY, especially if they are new to AutoFixture.Tumefy

© 2022 - 2024 — McMap. All rights reserved.