Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Fluent TestValidate未引发SetValidator列表上规则集的验证错误_C#_Unit Testing_.net Core_Mstest_Fluentvalidation - Fatal编程技术网

C# Fluent TestValidate未引发SetValidator列表上规则集的验证错误

C# Fluent TestValidate未引发SetValidator列表上规则集的验证错误,c#,unit-testing,.net-core,mstest,fluentvalidation,C#,Unit Testing,.net Core,Mstest,Fluentvalidation,当使用Fluent的testhelpers-TestValidate方法对我的规则进行单元测试时,当对象列表为null时,它抛出TestValidationException。验证程序设置指定NotNull规则 验证器如下所示 public class RequestValidator : AbstractValidator<Request> { public RulebookRequestValidator() { RuleFor(x => x

当使用Fluent的testhelpers-TestValidate方法对我的规则进行单元测试时,当对象列表为null时,它抛出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.
    }
}

这是在单元测试中测试列表上的子验证器的正确方法吗?正在寻找使用TestValidate对该规则进行单元测试的方法。

您的验证器工作正常,但有一个小问题

让我们看看这条规则:

RuleForEach(x=>x.EntryRequests).SetValidator(新的EntryRequestValidator()).NotNull()

它基本上是说:对
EntryRequest
集合中的每个元素运行NotNull检查(并执行EntryRequestValidator)

它不起作用,因为您的集合是空的:
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=null
  • EntryRequests=空列表
  • 此列表的任何元素都为空
加上来自嵌套验证器的所有验证

公共类RulebookRequestValidator:AbstractValidator
public class RulebookRequestValidator : AbstractValidator<Request>
{
    public RulebookRequestValidator()
    {
        RuleFor(x => x.Id).NotEmpty();
        RuleFor(x => x.Name).NotEmpty();
        RuleFor(x => x.EntryRequests).NotNull()
            .ForEach(element => element.SetValidator(new EntryRequestValidator()).NotNull());
    }
}
{ 公共RulebookRequestValidator() { RuleFor(x=>x.Id).NotEmpty(); RuleFor(x=>x.Name).NotEmpty(); (x=>x.EntryRequests).NotNull()的规则 .ForEach(element=>element.SetValidator(newentryrequestvalidator()).NotNull()); } }
您需要检查对象是否为null,然后为集合元素设置验证器

为了便于阅读,您可以定义自定义规则生成器扩展方法

public static IRuleBuilderOptions<T, IEnumerable<TK>> MustBeValidCollection<T, TK>(
        this IRuleBuilder<T, IEnumerable<TK>> ruleBuilder,
        AbstractValidator<TK> validator)
    {
        return ruleBuilder.NotNull()
            .ForEach(collection => collection.NotNull().SetValidator(validator));
    }
公共静态IRuleBuilderOptions必须是有效集合(
这个IRuleBuilder规则生成器,
抽象验证器(验证器)
{
返回ruleBuilder.NotNull()
.ForEach(collection=>collection.NotNull().SetValidator(validator));
}

并像
RuleFor(x=>x.EntryRequests)一样使用它

谢谢你的推荐,我喜欢你推荐的扩展:)+1。实际上,这两种解决方案对我都有效:)感谢您解释详细信息并指出我的解决方案中的错误:)
public class RulebookRequestValidator : AbstractValidator<Request>
{
    public RulebookRequestValidator()
    {
        RuleFor(x => x.Id).NotEmpty();
        RuleFor(x => x.Name).NotEmpty();
        RuleFor(x => x.EntryRequests).NotNull()
            .ForEach(element => element.SetValidator(new EntryRequestValidator()).NotNull());
    }
}
public static IRuleBuilderOptions<T, IEnumerable<TK>> MustBeValidCollection<T, TK>(
        this IRuleBuilder<T, IEnumerable<TK>> ruleBuilder,
        AbstractValidator<TK> validator)
    {
        return ruleBuilder.NotNull()
            .ForEach(collection => collection.NotNull().SetValidator(validator));
    }