Customizing for a base class or interface

Jun 27, 2011 at 2:56 PM

I've tried to make a customization for an entity base class where it should omit the ID property when creating the fixture like this:

fixture.Customize<AggregateRoot>(x => x.Without(y => y.Id));

I hoped it would omit the ID property for all descendants of AggregateRoot, but it does not.

I assume it can be done, given the extensibility I've seen so far ;) ... but what's the best way to do it? 

//Asger

Coordinator
Jun 27, 2011 at 3:59 PM

See this issue.

Jun 28, 2011 at 9:54 AM

Thanks! :)

Developer
Nov 21, 2011 at 10:10 AM
Edited Nov 21, 2011 at 2:19 PM

I just came across the same scenario today. :)

Following this article http://blog.ploeh.dk/2010/10/19/ConventionbasedCustomizationsWithAutoFixture.aspx it is possible to write a convention-based customization as below:

(Assuming that the Id property is a Guid)

 

public class EntitySpecimenBuilder : ISpecimenBuilder
{
    public object Create(object request, ISpecimenContext context)
    {
        var pi = request as PropertyInfo;
        if (pi == null)
        {
            return new NoSpecimen(request);
        }
        if (pi.PropertyType != typeof(Guid) || pi.Name != "Id")
        {
            return new NoSpecimen(request);
        }

        return Guid.Empty;
    }
}