我在我的单元和集成测试中使用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
}
Run Code Online (Sandbox Code Playgroud)
它只是将提供的字符串限制为某个值(由于其他原因,我无法使用直接枚举).
如何自定义Autofixture以创建适当的数据?
目前,AutoFixture 支持以下验证属性:
要支持自定义验证属性,您必须从以下组之一进行推断:
然后,您必须将新创建的 Relay 和 Generator 类添加为Customizations.
| 归档时间: |
|
| 查看次数: |
1153 次 |
| 最近记录: |