, . 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;
if (pi != null && pi.PropertyType == typeof(string))
{
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;
return generatedString;
}
if (Attribute.IsDefined(pi, typeof(AlphaNumericAttribute)))
{
return generatedString;
}
return result;
}
return result;
}
AutoFixture :
Fixture.Customizations.Add(new CustomAnnotationsBuilder());
Fixture.Customize(new NoDataAnnotationsCustomization());
!