我有一个看起来像这样的验证器
public class ImageValidator : AbstractValidator<Image>
{
public ImageValidator()
{
RuleFor(e => e.Name).NotEmpty().Length(1, 255).WithMessage("Name must be between 1 and 255 chars");
RuleFor(e => e.Data).NotNull().WithMessage("Image must have data").When(e => e.Id == 0);
RuleFor(e => e.Height).GreaterThan(0).WithMessage("Image must have a height");
RuleFor(e => e.Width).GreaterThan(0).WithMessage("Image must have a width");
}
}
现在我的单元测试失败了,因为高度和宽度是根据数据中的值填充的。
Data 包含一个字节数组,它创建一个位图图像。如果 Data 不为空(并且 Id 等于 0,因此它是一个新图像),我可以创建一个位图图像并获取高度和宽度值。更新后,Height 和 Width 已经被填充,Data 可能为空,因为它已经存储在数据库中。
如果数据的验证规则在我的验证器中为真,我是否可以填充高度和宽度的值?
在我有支票之前
if (myObject.Data == null) throw new ApplicationException(...
但我认为这是一个验证规则,应该在验证器中,还是我只需要拆分规则?