public void Validate_RequiredAndNotNullAtTopLevel_Valid() { // Arrange var metadata = _metadataProvider.GetMetadataForProperty(typeof(string), "Length"); var validator = new DataAnnotationsModelValidator( new ValidationAttributeAdapterProvider(), new RequiredAttribute(), stringLocalizer: null); var validationContext = new ModelValidationContext( actionContext: new ActionContext(), modelMetadata: metadata, metadataProvider: _metadataProvider, container: null, model: 123); // Act var result = validator.Validate(validationContext); // Assert Assert.Empty(result); }
public void Validate_SetsMemberName_AsExpected( ModelMetadata metadata, object container, object model, string expectedMemberName) { // Arrange var attribute = new Mock <TestableValidationAttribute> { CallBase = true }; attribute .Setup(p => p.IsValidPublic(It.IsAny <object>(), It.IsAny <ValidationContext>())) .Callback((object o, ValidationContext context) => { Assert.Equal(expectedMemberName, context.MemberName); }) .Returns(ValidationResult.Success) .Verifiable(); var validator = new DataAnnotationsModelValidator( new ValidationAttributeAdapterProvider(), attribute.Object, stringLocalizer: null); var validationContext = new ModelValidationContext( actionContext: new ActionContext(), modelMetadata: metadata, metadataProvider: _metadataProvider, container: container, model: model); // Act var results = validator.Validate(validationContext); // Assert Assert.Empty(results); attribute.VerifyAll(); }
public void Validate_RequiredButNullAtTopLevel_Invalid() { // Arrange var metadata = _metadataProvider.GetMetadataForProperty(typeof(string), "Length"); var validator = new DataAnnotationsModelValidator( new ValidationAttributeAdapterProvider(), new RequiredAttribute(), stringLocalizer: null); var validationContext = new ModelValidationContext( actionContext: new ActionContext(), modelMetadata: metadata, metadataProvider: _metadataProvider, container: null, model: null); // Act var result = validator.Validate(validationContext); // Assert var validationResult = result.Single(); Assert.Empty(validationResult.MemberName); Assert.Equal(new RequiredAttribute().FormatErrorMessage("Length"), validationResult.Message); }