c# - Fluent TestValidate 不会为使用 SetValidator 在 List 上设置的规则引发验证错误
问题描述
在使用 Fluent 的测试助手 TestValidate 方法对我的规则进行单元测试时,当对象列表为空时,它会抛出 TestValidationException。验证器设置指定NotNull规则。
验证器如下所示
public class RequestValidator : AbstractValidator<Request>
{
public RulebookRequestValidator()
{
RuleFor(x => x.Id).NotEmpty();
RuleFor(x => x.Name).NotEmpty();
RuleForEach(x => x.EntryRequests).SetValidator(new EntryRequestValidator()).NotNull(); // Would like to validate this rule when EntryRequests is null.
}
}
子验证器看起来像这样,
public class EntryRequestValidator : AbstractValidator<EntryRequest>
{
public EntryRequestValidator()
{
RuleFor(x => x.EntryRequestId).NotEmpty();
RuleForEach(x => x.Messages).NotNull().SetValidator(new MessagesValidator());
}
}
在我的单元测试中
[TestMethod]
public void Should_Have_Error_When_Object_Is_Empty()
{
// arrange
Request model = new () { EntryRequests = new () };
var validator = new RequestValidator();
// assert
var result = validator.TestValidate(model);
// act
Assert.IsFalse(result.IsValid);
result.ShouldHaveValidationErrorFor(x => x.Id)
.WithErrorCode(NotEmptyValidatorErrorCode);
result.ShouldHaveValidationErrorFor(x => x.Name)
.WithErrorCode(NotEmptyValidatorErrorCode);
result.ShouldHaveValidationErrorFor(x => x.EntryRequests); // throws ValidateTestException.
}
这是在单元测试列表中测试子验证器的正确方法吗?寻找一种方法来使用 TestValidate 对这个规则进行单元测试。
解决方案
您的验证器工作正常,但有一个小问题。
让我们看看这个规则:
RuleForEach(x => x.EntryRequests).SetValidator(new EntryRequestValidator()).NotNull();
它基本上说:对集合中的每个元素运行 NotNull 检查(并执行 EntryRequestValidator)EntryRequest
。
它不起作用,因为您的收藏是空的:EntryRequests = new ()
。它没有元素,所以没有什么要验证的。
将其更改为:
RuleFor(x => x.EntryRequests).NotEmpty(); // throws validation error when collection is null or empty
RuleForEach(x => x.EntryRequests).SetValidator(new EntryRequestValidator()).NotNull();
使用上述规则,您将在以下情况下收到验证错误:
- 入口请求 = 空
- EntryRequests = 空列表
- 此列表的任何元素为空
加上来自嵌套验证器的所有验证
推荐阅读
- tensorflow - 我用 'tf.keras.Sequential()' 构建的模型不起作用,为什么?
- javascript - Make In App Browser 请求地理定位权限
- c# - 将 CSR.txt 证书转换为 .pem
- python - 回顾数据框中的前一行并选择特定记录
- azure-functions - 不知道为什么我的 Azure CLI 在创建 azure http 触发函数时没有将 python 显示为语言选项
- android - Android异常-我写入文件时的ArrayIndexOutOfBoundsException kotlin
- python - Python Selenium Chrome AttributeError:“str”对象没有属性“id”
- scala - 如何强制 Spark 只执行一次转换?
- python - 使用 PyCharm 加载图像
- android - 文件的 Chaquopy 包/模块在哪里放置