如何单元测试的简单属性已验证设置? [英] How to unit test simple property has validator set?

查看:157
本文介绍了如何单元测试的简单属性已验证设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在多个模型对象的一些性质类似的规则,我想用自定义属性验证来取代他们,以避免在单元测试代码重复。

I have similar rules for some properties in multiple model objects and I want to replace them with custom property validators to avoid code duplication in unit tests.

我有我的财产验证:

public class IntIdPropertyValidator: PropertyValidator
{
    public IntIdPropertyValidator()
        : base("Property {PropertyName} should be greater than 0")
    {
    }

    protected override bool IsValid(PropertyValidatorContext context)
    {
        var value = (int)context.PropertyValue;

        return value > 0;
    }
}

和模型验证器类布线起来:

And wiring it up in model validator class:

public class SomeRequestValidator : AbstractValidator<CreateWordRequest>
{
    public SomeRequestValidator()
    {
        RuleFor(x => x.Id).SetValidator(new IntIdPropertyValidator());
    }
}



试图测试:

Tried to test:

[Test]
public void Validate_IdHasValidator_Success()
{
    Init();

    validator.ShouldHaveChildValidator(x => x.Id, typeof(IntIdPropertyValidator));
}



但测试总是失败。

But test always fails.

所以,我怎么能测试验证实际上是财产标识设置?

So, how can I test that validator is actually set for property Id?

推荐答案

您正在使用 ShouldHaveChildValidator 在错误的道路。 编号是一个简单的类型。

You are using ShouldHaveChildValidator in the wrong way. Id is a simple type.

ShouldHaveChildValidator 是在复杂的类型使用之中。 (另见源代码

ShouldHaveChildValidator is being in used on complex types. (see also the source code)

测试属性的正确方法是通过有效的对象和无效的对象,然后varify使用 ShouldNotHaveValidationErrorFor ShouldHaveValidationErrorFor

The right way to test the property is to pass valid objects and invalid objects and then varify using ShouldNotHaveValidationErrorFor and ShouldHaveValidationErrorFor:

[Test]
public void Should_have_error_when_Id_Is_Ilegal() {
      validator.ShouldHaveValidationErrorFor(p => p.Id, new CreateWordRequest());
}

[Test]
public void Should_not_have_error_when_Id_Is_Legal() {
      validator.ShouldNotHaveValidationErrorFor(p => p.Id, new CreateWordRequest()
                                                           {
                                                                Id = 7
                                                           });
}

修改

下面的代码会做你要找的验证:

The following code will do the verification you were looking for:

[Test]
public void Validate_IdHasValidator_Success()
{
    var validator = new SomeRequestValidator();

    var descriptor = validator.CreateDescriptor();
    var matchingValidators = descriptor.GetValidatorsForMember(
                Extensions.GetMember<CreateWordRequest, int>(x => x.Id).Name);

    Assert.That(matchingValidators.FirstOrDefault(), Is.InstanceOf<IntIdPropertyValidator>());

}



我想解释一下你的理由,你不该'T使用上面的代码。

I'd like to explain you the reason that you shouldn't use the above code.

在UT类,你验证类的行为将不会受到伤害。

When you UT class you verify that the class behavior won't be harmed.

当你创建一个自定义验证,你创建一个类有责任核实具体型号( - >业务规则)...

When you create a custom validator, you create a class with a responsibility to verify specific model( --> business rules)...

标识是一个简单的类型与根据他的父模型业务规则。
因此,您需要通过模型验证,验证 Id的业务规则。

Id is a simple type with a business rules according to his parent model. Therefore you need to verify the business rules of Id through the model validator.

让我们假设您的机型之一突然需要改变。在这种情况下,你没有任何的你现有的业务规则将不会伤害任何验证(或您决定在 IntIdPropertyValidator 的变化,这样的举措会影响到任何地方,即使你不想)。

Let's assume that one of your models suddenly need to change. In this case you don't have any validation that any of you existing business rules won't harmed(or you decide to make changes inside IntIdPropertyValidator, such a move will affect anywhere, even if you didn't want to).

创建自定义的属性验证是代码维护非常好然而,测试应该是针对模型验证

Creating a custom Property Validator is very good for code maintenance however, the tests should be against the model validator.

在复杂类型的故事是完全不同的:

On complex types the story is quite different:

通常复杂类型都有自己的业务规则。在这种情况下,你必须创建一个自定义验证他们,然后验证父验证使用正确的验证。验证另一件事是:如果复杂的数据类型为或复杂的规则,如当属性值是X和再复杂类型的状态是Y...

Usually complex types has their own business rules. In this case, you have to create a custom validator for them, and then verify that the parent validator use the right validator. Another thing to verify is: If the complex type is Null or complex rules such as "when the property value is X and then complex type state is Y"...

这篇关于如何单元测试的简单属性已验证设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆