Wednesday, April 27, 2022

Using AutoFixture To Create Early Bound Entities

@AutoFixtureAutoFixture is an open source library that is used in testing to create objects without having to explicit set all the values.  I recently attempted to use it in a unit test to create an instance of an early bound entity, and assumed it would be extremely trivial, but boy was a wrong.  But now at least, you have the “joy” of reading this blog post about it.




The Problem(s)

This is what attempting to use an AutoFixture straight out of the box to create an entity looks like:

[TestMethod]
public void EarlyBoundAutoFixture_Should_Generate()
{   
    var fixture = new Fixture();
    // Fails here:
    // AutoFixture.ObjectCreationExceptionWithPath: 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.
    var contact = fixture.Create<Contact>();
    Assert.IsNotNull(contact.FirstName);
}

The error basically AutoFixture can’t create the ExtensionDataObject since it does not expose a public constructor.  OK, makes sense.  The simplest thing to do is to make a fluent build call and skip the property, but this doesn’t work because other types like Money, have the ExtensionData property and it will fail for those properties as well, and manually skip the ExtensionData property on every object would make AutoFixture viturally worthless.  The solution is to create an ISpecimenBuilder that tells AutoFixture how to create an ExtensionData (in actuality don’t, just set it to null).  This looks like this:

[TestMethod]
public void EarlyBoundAutoFixture_Should_Generate()
{
    var fixture = new Fixture();
    fixture.Customizations.Add(new SkipExtensionData());
   
    // New error
    // AutoFixture.ObjectCreationExceptionWithPath: AutoFixture was unable to create an instance of type AutoFixture.Kernel.FiniteSequenceRequest
    // because the traversed object graph contains a circular reference. Information about the circular path follows below. This is the correct
    // behavior when a Fixture is equipped with a ThrowingRecursionBehavior, which is the default. This ensures that you are being made aware of
    // circular references in your code. Your first reaction should be to redesign your API in order to get rid of all circular references.
    // However, if this is not possible (most likely because parts or all of the API is delivered by a third party), you can replace this default
    // behavior with a different behavior: on the Fixture instance, remove the ThrowingRecursionBehavior from Fixture.Behaviors, and instead add
    // an instance of OmitOnRecursionBehavior:
    //
    //   fixture.Behaviors.OfType<ThrowingRecursionBehavior>().ToList()
    //       .ForEach(b => fixture.Behaviors.Remove(b));
    //   fixture.Behaviors.Add(new OmitOnRecursionBehavior());
    var contact = fixture.Create<Contact>();     Assert.IsNotNull(contact.FirstName);
}


public class SkipExtensionData : ISpecimenBuilder
{
    public object Create(object request, ISpecimenContext context)
    {
        var pi = request as PropertyInfo;
        if (pi == null)
        {
            return new NoSpecimen();
        }

        if (typeof(ExtensionDataObject).IsAssignableFrom(pi.PropertyType))
        {
            return null;
        }

        return new NoSpecimen();
    }
}

But once again, a new error is generated.  This time a circular reference error.  Extra points to the team at AutoFixture for putting the solution to the issue in the code. But after adding it, more issues still pop up.

[TestMethod]
public void EarlyBoundAutoFixture_Should_Generate()
{
    var fixture = new Fixture();
    fixture.Customizations.Add(new SkipExtensionData());
    fixture.Behaviors.OfType<ThrowingRecursionBehavior>().ToList()
        .ForEach(b => fixture.Behaviors.Remove(b));
    fixture.Behaviors.Add(new OmitOnRecursionBehavior());

    // Yet another error:
    // System.InvalidOperationException: Sequence contains no elements
    // Stack Trace:
    //   Enumerable.First[TSource](IEnumerable`1 source)
    //   Entity.SetRelatedEntities[TEntity](String relationshipSchemaName, Nullable`1 primaryEntityRole, IEnumerable`1 entities)
    //   Contact.set_ReferencedContact_Customer_Contacts(IEnumerable`1 value) line 6219
    var contact = fixture.Create<Contact>();

    Assert.IsNotNull(contact.FirstName);
}

This is a fun error where when setting a related entity collection to an empty collection, you get a Sequence contains no elements error.  (Which I could possible handle in the Early Bound Generator I guess) but this calls out something that in my opinion shouldn’t be getting populated, child collections of entities.  Only the properties of the entity that are actual properties and not LINQ relationships needs to be populated, so we can actually remove the recursive behavior check and resolve this final issue by tweaking the ISpecimenBuilder to skip these types of properties, which brings us to the first solution that doesn’t throw an exception:

[TestMethod]
public void EarlyBoundAutoFixture_Should_Generate()
{
    var fixture = new Fixture();
    fixture.Customizations.Add(new SkipEntityProperties());

    var contact = fixture.Create<Contact>();

    // Fails!  FirstName is Null
    Assert.IsNotNull(contact.FirstName);
}

public class SkipEntityProperties: ISpecimenBuilder
{
    public object Create(object request, ISpecimenContext context)
    {
        var pi = request as PropertyInfo;
        if (pi == null)
        {
            return new NoSpecimen();
        }

        if (typeof(ExtensionDataObject).IsAssignableFrom(pi.PropertyType))
        {
            return null;
        }

        if (pi.DeclaringType == typeof(Entity))
        {
            return null;
        }

        // Property is for an Entity Class, and the Property has a generic type parameter that is an entity, or is an entity
        if (typeof(Entity).IsAssignableFrom(pi.DeclaringType)
            &&
            (pi.PropertyType.IsGenericType && pi.PropertyType.GenericTypeArguments.Any(t => typeof(Entity).IsAssignableFrom(t))
             || typeof(Entity).IsAssignableFrom(pi.PropertyType)
             )
           )
        {
            return null;
        }

        return new NoSpecimen();
    }
}

It was at this point that I couldn’t understand what was going on.  Why aren’t these values getting populated?  2 hours of debugging latter I finally realized that AutoFixture was setting the AttributeCollection of the Entity to null, effectively removing all other variables that were just being set by AutoFixture.  Some more internet researching later I discovered that there was an OmitSpecimen value that would leave the value untouched!  Armed this this knowledge the final solution presented itself!

The Solution

This final bit of code will correctly populate the attributes of the early bound entity:

[TestMethod]
public void EarlyBoundAutoFixture_Should_Generate()
{
    var fixture = new Fixture();
    fixture.Customizations.Add(new SkipEntityProperties());

    var contact = fixture.Create<Contact>();

    Assert.IsNotNull(contact.FirstName);
}

public class SkipEntityProperties: ISpecimenBuilder
{
    public object Create(object request, ISpecimenContext context)
    {
        var pi = request as PropertyInfo;
        if (pi == null)
        {
            return new NoSpecimen();
        }

        if (typeof(ExtensionDataObject).IsAssignableFrom(pi.PropertyType))
        {
            return new OmitSpecimen();
        }

        if (pi.DeclaringType == typeof(Entity))
        {
            return new OmitSpecimen();
        }

        // Property is for an Entity Class, and the Property has a generic type parameter that is an entity, or is an entity
        if (typeof(Entity).IsAssignableFrom(pi.DeclaringType)
            &&
            (pi.PropertyType.IsGenericType && pi.PropertyType.GenericTypeArguments.Any(t => typeof(Entity).IsAssignableFrom(t))
             || typeof(Entity).IsAssignableFrom(pi.PropertyType)
             || typeof(AttributeCollection).IsAssignableFrom(pi.PropertyType)
             )
           )
        {
            return new OmitSpecimen();
        }

        return new NoSpecimen();
    }
}

Here is an example screen shot from above:
image

Notice how everything except the AccountId (Since it’s readonly) has been automatically populated with a default value?  It’s a beautiful thing!

If you found this help, please share it!

No comments:

Post a Comment