AutoComplete and User Data

I use AutoFixture in my module and integration tests and ran into a problem. I create data transfer objects, and some of these classes have DataAnnotation attributes (some of which are normal) in the properties. AutoFixture sees these and does not generate data for them, apparently because it is not sure what data it expects.

An example of a custom validation attribute that I use:

public class Person 
{
   public string FullName { get; set; }

   [Enum("M", "F")]
   public string Gender { get; set; }
}

public class EnumAttribute : ValidationAttribute
{
   public EnumAttribute(params string[] allowedStrings)
    {
        if (allowedStrings == null || allowedStrings.Length == 0)
            throw new ArgumentException("allowedStrings");
        AllowNull = true;
        AllowedStrings = allowedStrings;
        ErrorMessage = "The field '{0}' is invalid. Allowed strings are " + string.Join(", ", AllowedStrings);
    }

    // ... implementation
}

It simply limits the provided string to a specific value (I could not use direct enumeration for other reasons).

How can I configure Autofixture to create relevant data?

+4
source share
2 answers

, . Relay, Request Generator , . ISpecimenBuilder, .

. , , , AlphaNumericAttribute EnumAttribute.

- :

public class CustomAnnotationsBuilder : ISpecimenBuilder
{
    private readonly Random rnd = new Random();

    public object Create(object request, ISpecimenContext context)
    {
        object result = new NoSpecimen(request);

        var pi = request as PropertyInfo;

        // do a few different inspections if it a string
        if (pi != null && pi.PropertyType == typeof(string))
        {                
            // handle Enum attributes
            if (Attribute.IsDefined(pi, typeof(EnumAttribute)))
            {
                var enumAttribute = (EnumAttribute)Attribute.GetCustomAttribute(
                    pi, typeof(EnumAttribute));
                var allowedStrings = enumAttribute.GetAllowedStrings();
                return allowedStrings[rnd.Next(0, allowedStrings.Length)];
            }                

            if (Attribute.IsDefined(pi, typeof(StringLengthAttribute)))
            {
                var stringLengthAttribute = (StringLengthAttribute)Attribute.GetCustomAttribute(
                    pi, typeof(StringLengthAttribute));
                minLength = stringLengthAttribute.MinimumLength;
                maxLength = stringLengthAttribute.MaximumLength;

                // do custom string generation here
                return generatedString;
            }

            if (Attribute.IsDefined(pi, typeof(AlphaNumericAttribute)))
            {
                // do custom string generation here
                return generatedString;
            }

            return result;
        }

        return result;
}

AutoFixture :

Fixture.Customizations.Add(new CustomAnnotationsBuilder());
Fixture.Customize(new NoDataAnnotationsCustomization()); // ignore data annotations since I'm handling them myself

!

+3

All Articles