My builder is set up to either deal with a Parameter or a Property. This may change in the future, but for now this is what I have in my builder:
public class UserNameBuilder : ISpecimenBuilder
{
public object Create(object request, ISpecimenContext context)
{
var propertyInfo = request as PropertyInfo;
if (propertyInfo != null && propertyInfo.Name == "UserName" && propertyInfo.PropertyType == typeof(string))
{
return GetUserName();
}
var parameterInfo = request as ParameterInfo;
if (parameterInfo != null && parameterInfo.Name == "userName" && parameterInfo.ParameterType == typeof(string))
{
return GetUserName();
}
return new NoSpecimen(request);
}
static object GetUserName()
{
var fixture = new Fixture();
return new SpecimenContext(fixture).Resolve(new RegularExpressionRequest(@"^[a-zA-Z0-9_.]{6,30}$"));
}
}
My UserName object is a ValueType object and is as follows:
public class UserName : SemanticType<string>
{
private static readonly Regex ValidPattern = new Regex(@"^[a-zA-Z0-9_.]{6,30}$");
public UserName(string userName) : base(IsValid, userName)
{
Guard.NotNull(() => userName, userName);
Guard.IsValid(() => userName, userName, IsValid, "Invalid username");
}
public static bool IsValid(string candidate)
{
return ValidPattern.IsMatch(candidate);
}
public static bool TryParse(string candidate, out UserName userName)
{
userName = null;
try
{
userName = new UserName(candidate);
return true;
}
catch (ArgumentException ex)
{
return false;
}
}
}
The UserName class inherits from SemanticType which is a project that provides a base for my value types.
Whenever I use AutoFixture as follows:
var fixture = new Fixture();
fixture.Customizations.Add(new UserNameBuilder());
var userName = fixture.Create<UserName>();
I always get the value "......" I thought I would get a different value each time. Is what I'm seeing expected behavior?
Thanks
Aucun commentaire:
Enregistrer un commentaire