Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 730c683da2
Fetching contributors…

Cannot retrieve contributors at this time

119 lines (99 sloc) 4.895 kb
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.Http.Metadata;
using System.Web.Http.Metadata.Providers;
using Moq;
using Moq.Protected;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;
namespace System.Web.Http.Validation.Validators
{
public class DataAnnotationsModelValidatorTest
{
private static DataAnnotationsModelMetadataProvider _metadataProvider = new DataAnnotationsModelMetadataProvider();
private static IEnumerable<ModelValidatorProvider> _noValidatorProviders = Enumerable.Empty<ModelValidatorProvider>();
[Fact]
public void ConstructorGuards()
{
// Arrange
ModelMetadata metadata = _metadataProvider.GetMetadataForType(null, typeof(object));
var attribute = new RequiredAttribute();
// Act & Assert
Assert.ThrowsArgumentNull(
() => new DataAnnotationsModelValidator(null, attribute),
"validatorProviders");
Assert.ThrowsArgumentNull(
() => new DataAnnotationsModelValidator(_noValidatorProviders, null),
"attribute");
}
[Fact]
public void ValuesSet()
{
// Arrange
ModelMetadata metadata = _metadataProvider.GetMetadataForProperty(() => 15, typeof(string), "Length");
var attribute = new RequiredAttribute();
// Act
var validator = new DataAnnotationsModelValidator(_noValidatorProviders, attribute);
// Assert
Assert.Same(attribute, validator.Attribute);
}
[Fact]
public void ValidateWithIsValidTrue()
{
// Arrange
ModelMetadata metadata = _metadataProvider.GetMetadataForProperty(() => 15, typeof(string), "Length");
Mock<ValidationAttribute> attribute = new Mock<ValidationAttribute> { CallBase = true };
attribute.Setup(a => a.IsValid(metadata.Model)).Returns(true);
DataAnnotationsModelValidator validator = new DataAnnotationsModelValidator(_noValidatorProviders, attribute.Object);
// Act
IEnumerable<ModelValidationResult> result = validator.Validate(metadata, null);
// Assert
Assert.Empty(result);
}
[Fact]
public void ValidateWithIsValidFalse()
{
// Arrange
ModelMetadata metadata = _metadataProvider.GetMetadataForProperty(() => 15, typeof(string), "Length");
Mock<ValidationAttribute> attribute = new Mock<ValidationAttribute> { CallBase = true };
attribute.Setup(a => a.IsValid(metadata.Model)).Returns(false);
DataAnnotationsModelValidator validator = new DataAnnotationsModelValidator(_noValidatorProviders, attribute.Object);
// Act
IEnumerable<ModelValidationResult> result = validator.Validate(metadata, null);
// Assert
var validationResult = result.Single();
Assert.Equal("", validationResult.MemberName);
Assert.Equal(attribute.Object.FormatErrorMessage("Length"), validationResult.Message);
}
[Fact]
public void ValidatateWithValidationResultSuccess()
{
// Arrange
ModelMetadata metadata = _metadataProvider.GetMetadataForProperty(() => 15, typeof(string), "Length");
Mock<ValidationAttribute> attribute = new Mock<ValidationAttribute> { CallBase = true };
attribute.Protected()
.Setup<ValidationResult>("IsValid", ItExpr.IsAny<object>(), ItExpr.IsAny<ValidationContext>())
.Returns(ValidationResult.Success);
DataAnnotationsModelValidator validator = new DataAnnotationsModelValidator(_noValidatorProviders, attribute.Object);
// Act
IEnumerable<ModelValidationResult> result = validator.Validate(metadata, null);
// Assert
Assert.Empty(result);
}
[Fact]
public void IsRequiredTests()
{
// Arrange
ModelMetadata metadata = _metadataProvider.GetMetadataForProperty(() => 15, typeof(string), "Length");
// Act & Assert
Assert.False(new DataAnnotationsModelValidator(_noValidatorProviders, new RangeAttribute(10, 20)).IsRequired);
Assert.True(new DataAnnotationsModelValidator(_noValidatorProviders, new RequiredAttribute()).IsRequired);
Assert.True(new DataAnnotationsModelValidator(_noValidatorProviders, new DerivedRequiredAttribute()).IsRequired);
}
class DerivedRequiredAttribute : RequiredAttribute
{
}
}
}
Jump to Line
Something went wrong with that request. Please try again.