9

我有一个测试类,其中包含几个测试来检查实体是否IsValid. 我从拥有自己的自定义验证转向使用IValidatableObject,但我坚持使用正确的验证技术。

这是我的测试课:

[TestFixture]
public class StudentTests {
    private static Student GetContactWithContactInfo()
    {
        return new Student(new TestableContactRepository())
                            {
                                Phone = "7275551111"
                            };
    }

    private static Student GetContactWithoutContactInfo()
    {
        return new Student(new TestableContactRepository());
    }

    [Test]
    public void Student_Saving_StudentHasInfo_IsValid ()
    {
        // Arrange
        Student student = GetContactWithContactInfo();
        // Act
        student.Save();
        // Assert
        Assert.IsTrue(student.IsValid);
    }

    [Test]
    public void Student_Saving_StudentDoesNotHaveInfo_IsNotValid ()
    {
        // Arrange
        Student student = GetContactWithoutContactInfo();
        // Act
        student.Save();
        // Assert
        Assert.IsFalse(student.IsValid);
    }
}

这是我的实体:

public class Student : IValidatableObject
{
    private readonly IContactRepository contactRepository;

    public Student(IContactRepository _contactRepository)
    {
        contactRepository = _contactRepository;
        Contacts = new List<Student>();
    }

    [Required]
    public int Id { get; private set; }

    [StringLength(10, MinimumLength = 10)]
    public string Phone { get; set; }


    public List<Student> Contacts { get; private set; }

    public bool IsValid { get; private set; }

    public void Save()
    {
        if (IsValidForPersistance())
        {
            IsValid = true;
            Id = contactRepository.Save();
        }
    }

    private bool IsValidForPersistance()
    {
        return Validator.TryValidateObject(this, new ValidationContext(this), null, true);
    }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (string.IsNullOrEmpty(Phone) && Contacts.All(c => string.IsNullOrEmpty(c.Phone)))
            yield return new ValidationResult("The student or at least one contact must have a phone number entered", new[] { "Phone Number" });
    }
}

正如您可以看到IsValid通过调用IsValidForPersistance. Validate最终会有更多的验证。

上述测试都使用此方法通过,但下面的测试也通过但不应该通过。

[Test]
public void Student_Saving_HasContactInfoWithInvalidLength_IsNotValid()
{
    // Arrange
    Contact student = GetContactWithoutContactInfo();
    student.Phone = "string";

    // Act
    student.Save();

    // Assert
    Assert.IsFalse(student.IsValid);
}

在这里,我设置了我自己Phone的无效长度字符串的值。我希望验证失败,因为StringLength注释设置为最小和最大 10 个字符。

为什么会这样过去?

更新 自定义验证出现问题,使用更改更新了代码。除了 nemesv 关于private在属性上没有修饰符的建议之外,Phone它现在可以工作了。我已将所有代码更新为正常工作。

4

1 回答 1

18

默认情况下,Validator.TryValidateObject仅检查RequiredAttributes(以及类型级别属性和IValidatableObject实现等其他内容)。

如果您需要验证所有属性StringLength,您需要将validateAllProperties方法的参数设置为true

private bool IsValidForPersistance() {
    return Validator.TryValidateObject(this, 
                                       new ValidationContext(this), 
                                       null,
                                       true /* validateAllProperties */);
}
于 2012-09-01T17:07:12.193 回答