【问题标题】:Testing custom validation of property depenent on other property returns ArgumentNullException测试依赖于其他属性的属性的自定义验证返回 ArgumentNullException
【发布时间】:2019-12-17 00:42:37
【问题描述】:

我正在尝试编写一个 XUnit 测试来测试我的自定义验证器。验证器检查其他属性的值,该值指示已验证的属性是否应该为 null 或具有值。但是,当我使用 TryValidateProperty 方法时,测试会返回 ArgumentNullException。

验证器:

public class ConcatenatedDataValidator : ValidationAttribute
{
    public string PropertyName { get; private set; }
    public ConcatenatedDataValidator(string propertyName)
    {
        this.PropertyName = propertyName;
    }


    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        var property = validationContext.ObjectType.GetProperty(PropertyName);
        if(property == null && value != null)
        {
            return new ValidationResult(string.Format("{0} is null", PropertyName));
        }
        var chosenValue = property.GetValue(validationContext.ObjectInstance, null);

        if(chosenValue.Equals("00") && (value == null || value.Equals(string.Empty))
            ||  chosenValue.Equals("01") && value != null) 
        {
            return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName));
        }

        return null;
    }  
}

测试:

public class ConcatenatedDataValidatorTests
{
    private TestedModel model;
    private ValidationContext context;
    private List<ValidationResult> results;

    [Fact]
    public void IsValid_OtherValueIs00ThisValueIsNull_ReturnFalse()
    {
        // arrange
        var concatenatedDataValidator = new ConcatenatedDataValidator("OtherProperty");
        model = new TestedModel();
        model.OtherProperty = "00";
        model.ThisProperty = null;
        context = new ValidationContext(model);
        results = new List<ValidationResult>();

        // act
        var result = Validator.TryValidateProperty(model.ThisProperty, context, results);

        // assert
        Assert.False(result);

    }
}

测试返回

System.ArgumentNullException : Value cannot be null. Parameter name: propertyName

在表演部分。我只想测试这一个属性,因为在模型中我有很多其他属性需要属性,我希望让测试尽可能简单,只测试我的自定义验证器。有什么办法可以解决这个问题吗?

【问题讨论】:

    标签: c# unit-testing data-annotations validationattribute


    【解决方案1】:

    这是预期的行为。正如Validator.TryValidateProperty 指定的那样,如果valuenull,则抛出System.ArgumentNullException

    如果您想测试这种行为/您的属性值为null 的情况,那么您应该捕获异常并检查它 - 尽管这基本上是在测试 .NET 框架。

    这也表明您可以在您的IsValid 方法中取消value == null 检查,因为它永远不会触发。

    更新

    整个错误是指在TryValidateProperty 内部调用的私有方法EnsureValidPropertyType(参见here)。

    这是因为ValidationContext.MemberName引起的。

    要解决此问题,您必须在创建 ValidationContext 期间设置 MemberName(如果 .NET 4.7.2 和更早版本)

    ValidationContext context = new ValidationContext(model)
    {
            MemberName = "ThisProperty"
    };
    

    或更改应用配置 (.NET 4.8) 中的默认行为。

    <configuration>
       <appSettings>
          <add key="aspnet:GetValidationMemberName" value="true" />
       </appSettings>
    </configuration>
    

    【讨论】:

    • 当我期望值不为空时,测试用例怎么样。当我在测试中键入model.ThisProperty = "asd"; 时,它仍然会产生相同的异常。并且它仍然谈论在构造函数中传递的 propertyName 参数。
    • 还有ValidationAttribute.IsValid方法不返回System.ArgumentNullException,也不表示传递的值不能为null。可能存在这样一种情况,即我只是没有将任何东西传递给该属性,即使我已经在另一个中表明我想在此处传递它。我基本上是在尝试做一个验证器,根据另一个属性的值使一个属性成为必需的。
    • 哦,是的,它指的是在TryValidateProperty 内部调用的私有方法EnsureValidPropertyType(参见here)。这是因为ValidationContext.MemberName 造成的。我用解决方案更新我的答案。
    • 是的,更新的解决方案完美运行。非常感谢 :) 编辑:在 .NET 4.6.1 上测试
    • @Lordzio 太好了。只是为了确保model.ThisProperty = null 仍然应该抛出异常。
    猜你喜欢
    • 2011-09-09
    • 2011-08-20
    • 1970-01-01
    • 1970-01-01
    • 2017-12-16
    • 2010-11-15
    • 1970-01-01
    • 2016-07-08
    • 1970-01-01
    相关资源
    最近更新 更多