【发布时间】:2014-08-05 20:26:41
【问题描述】:
我在单元和集成测试中使用 AutoFixture,但遇到了问题。我正在生成数据传输对象,其中一些类在属性上有 DataAnnotation 属性(其中一些是自定义的)。 AutoFixture 会看到这些,但不会为它们生成数据,大概是因为它不确定它所期望的数据是什么。
我正在使用的自定义验证属性示例:
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
}
它只是将提供的字符串限制为某个值(由于其他原因,我无法使用直接向上的枚举)。
如何自定义 AutoFixture 以创建适当的数据?
【问题讨论】:
标签: autofixture