1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
// 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 Moq;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;
namespace System.Web.Mvc.Test
{
public class AssociatedValidatorProviderTest
{
[Fact]
public void GetValidatorsGuardClauses()
{
// Arrange
ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(object));
Mock<AssociatedValidatorProvider> provider = new Mock<AssociatedValidatorProvider> { CallBase = true };
// Act & Assert
Assert.ThrowsArgumentNull(
() => provider.Object.GetValidators(null, new ControllerContext()),
"metadata");
Assert.ThrowsArgumentNull(
() => provider.Object.GetValidators(metadata, null),
"context");
}
[Fact]
public void GetValidatorsForPropertyWithLocalAttributes()
{
// Arrange
IEnumerable<Attribute> callbackAttributes = null;
ControllerContext context = new ControllerContext();
ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForProperty(null, typeof(PropertyModel), "LocalAttributes");
Mock<TestableAssociatedValidatorProvider> provider = new Mock<TestableAssociatedValidatorProvider> { CallBase = true };
provider.Setup(p => p.AbstractGetValidators(metadata, context, It.IsAny<IEnumerable<Attribute>>()))
.Callback<ModelMetadata, ControllerContext, IEnumerable<Attribute>>((m, c, attributes) => callbackAttributes = attributes)
.Returns(() => null)
.Verifiable();
// Act
provider.Object.GetValidators(metadata, context);
// Assert
provider.Verify();
Assert.True(callbackAttributes.Any(a => a is RequiredAttribute));
}
[Fact]
public void GetValidatorsForPropertyWithMetadataAttributes()
{
// Arrange
IEnumerable<Attribute> callbackAttributes = null;
ControllerContext context = new ControllerContext();
ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForProperty(null, typeof(PropertyModel), "MetadataAttributes");
Mock<TestableAssociatedValidatorProvider> provider = new Mock<TestableAssociatedValidatorProvider> { CallBase = true };
provider.Setup(p => p.AbstractGetValidators(metadata, context, It.IsAny<IEnumerable<Attribute>>()))
.Callback<ModelMetadata, ControllerContext, IEnumerable<Attribute>>((m, c, attributes) => callbackAttributes = attributes)
.Returns(() => null)
.Verifiable();
// Act
provider.Object.GetValidators(metadata, context);
// Assert
provider.Verify();
Assert.True(callbackAttributes.Any(a => a is RangeAttribute));
}
[Fact]
public void GetValidatorsForPropertyWithMixedAttributes()
{
// Arrange
IEnumerable<Attribute> callbackAttributes = null;
ControllerContext context = new ControllerContext();
ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForProperty(null, typeof(PropertyModel), "MixedAttributes");
Mock<TestableAssociatedValidatorProvider> provider = new Mock<TestableAssociatedValidatorProvider> { CallBase = true };
provider.Setup(p => p.AbstractGetValidators(metadata, context, It.IsAny<IEnumerable<Attribute>>()))
.Callback<ModelMetadata, ControllerContext, IEnumerable<Attribute>>((m, c, attributes) => callbackAttributes = attributes)
.Returns(() => null)
.Verifiable();
// Act
provider.Object.GetValidators(metadata, context);
// Assert
provider.Verify();
Assert.True(callbackAttributes.Any(a => a is RangeAttribute));
Assert.True(callbackAttributes.Any(a => a is RequiredAttribute));
}
[MetadataType(typeof(Metadata))]
private class PropertyModel
{
[Required]
public int LocalAttributes { get; set; }
public string MetadataAttributes { get; set; }
[Required]
public double MixedAttributes { get; set; }
private class Metadata
{
[Range(10, 100)]
public object MetadataAttributes { get; set; }
[Range(10, 100)]
public object MixedAttributes { get; set; }
}
}
public abstract class TestableAssociatedValidatorProvider : AssociatedValidatorProvider
{
protected override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable<Attribute> attributes)
{
return AbstractGetValidators(metadata, context, attributes);
}
// Hoist access
public abstract IEnumerable<ModelValidator> AbstractGetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable<Attribute> attributes);
}
}
}
|