public void RegisterAdapterGuardClauses() { var provider = new DataAnnotationsModelValidatorProvider(); // Attribute type cannot be null Assert.ThrowsArgumentNull( () => provider.RegisterAdapter(null, typeof(MyValidationAttributeAdapter)), "attributeType"); // Adapter type cannot be null Assert.ThrowsArgumentNull( () => provider.RegisterAdapter(typeof(MyValidationAttribute), null), "adapterType"); // Validation attribute must derive from ValidationAttribute Assert.ThrowsArgument( () => provider.RegisterAdapter(typeof(object), typeof(MyValidationAttributeAdapter)), "attributeType", "The type Object must derive from ValidationAttribute"); // Adapter must derive from ModelValidator Assert.ThrowsArgument( () => provider.RegisterAdapter(typeof(MyValidationAttribute), typeof(object)), "adapterType", "The type Object must derive from ModelValidator"); // Adapter must have the expected constructor Assert.ThrowsArgument( () => provider.RegisterAdapter(typeof(MyValidationAttribute), typeof(MyValidationAttributeAdapterBadCtor)), "adapterType", "The type MyValidationAttributeAdapterBadCtor must have a public constructor which accepts three parameters of types ModelMetadata, IEnumerable<ModelValidatorProvider>, and MyValidationAttribute"); }
public void RegisterAdapter() { // Arrange var provider = new DataAnnotationsModelValidatorProvider(); provider.AttributeFactories = new Dictionary <Type, DataAnnotationsModelValidationFactory>(); // Act provider.RegisterAdapter( typeof(MyValidationAttribute), typeof(MyValidationAttributeAdapter) ); // Assert var type = provider.AttributeFactories.Keys.Single(); Assert.Equal(typeof(MyValidationAttribute), type); var factory = provider.AttributeFactories.Values.Single(); var metadata = _metadataProvider.GetMetadataForType(() => null, typeof(object)); var attribute = new MyValidationAttribute(); var validator = factory(_noValidatorProviders, attribute); Assert.IsType <MyValidationAttributeAdapter>(validator); }
public void RegisterAdapter() { // Arrange var provider = new DataAnnotationsModelValidatorProvider(); provider.AttributeFactories = new Dictionary<Type, DataAnnotationsModelValidationFactory>(); // Act provider.RegisterAdapter(typeof(MyValidationAttribute), typeof(MyValidationAttributeAdapter)); // Assert var type = provider.AttributeFactories.Keys.Single(); Assert.Equal(typeof(MyValidationAttribute), type); var factory = provider.AttributeFactories.Values.Single(); var metadata = _metadataProvider.GetMetadataForType(() => null, typeof(object)); var attribute = new MyValidationAttribute(); var validator = factory(_noValidatorProviders, attribute); Assert.IsType<MyValidationAttributeAdapter>(validator); }
public void RegisterAdapterGuardClauses() { var provider = new DataAnnotationsModelValidatorProvider(); // Attribute type cannot be null Assert.ThrowsArgumentNull( () => provider.RegisterAdapter(null, typeof(MyValidationAttributeAdapter)), "attributeType"); // Adapter type cannot be null Assert.ThrowsArgumentNull( () => provider.RegisterAdapter(typeof(MyValidationAttribute), null), "adapterType"); // Validation attribute must derive from ValidationAttribute Assert.ThrowsArgument( () => provider.RegisterAdapter(typeof(object), typeof(MyValidationAttributeAdapter)), "attributeType", "The type Object must derive from ValidationAttribute"); // Adapter must derive from ModelValidator Assert.ThrowsArgument( () => provider.RegisterAdapter(typeof(MyValidationAttribute), typeof(object)), "adapterType", "The type Object must derive from ModelValidator"); // Adapter must have the expected constructor Assert.ThrowsArgument( () => provider.RegisterAdapter(typeof(MyValidationAttribute), typeof(MyValidationAttributeAdapterBadCtor)), "adapterType", "The type MyValidationAttributeAdapterBadCtor must have a public constructor which accepts three parameters of types ModelMetadata, IEnumerable<ModelValidatorProvider>, and MyValidationAttribute"); }