FluentValidation's NotNull property is not working properly

48 Views Asked by At

Validation rule:

RuleFor(_ => _.SeverityType)
    .NotNull()
    .When(_ => _.ObservationType != null && _.ObservationType.IsInViolation)
    .WithMessage("Severity Type is required.");

MSTest test case:

[TestMethod]
public void SeverityType_ShouldNotHaveError_WhenNull()
{
    // Arrange
    var validator = new ViolationItemValidator();
    var observationType = new Business.Models.Violations.ObservationType { IsInViolation = true };
    var violationItem = new ViolationItemViewModel 
    { 
         SeverityType = null, 
         ObservationType = observationType 
    };

    // Act
    var result = validator.TestValidate(violationItem);

    // Assert
    result.ShouldNotHaveValidationErrorFor(_ => _.SeverityType);
}

Now though the SeverityType is null the test case is failing. Gives this result:

SeverityType_ShouldNotHaveError_WhenNull threw exception: FluentValidation.TestHelper.ValidationTestException: Expected no validation errors for property SeverityType

Validation Errors: [0]: Severity Type is required.

This same happen in Page as well, where I'm trying to update button visibility based on the Validation rules.

The button is not getting enabled though SeverityType is null. Anyone know how to fix this?

0

There are 0 best solutions below