public void RegisterAdapter() { var oldFactories = DataAnnotationsModelValidatorProvider.AttributeFactories; try { // Arrange DataAnnotationsModelValidatorProvider.AttributeFactories = new Dictionary <Type, DataAnnotationsModelValidationFactory>(); // Act DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(MyValidationAttribute), typeof(MyValidationAttributeAdapter)); // Assert var type = DataAnnotationsModelValidatorProvider.AttributeFactories.Keys.Single(); Assert.Equal(typeof(MyValidationAttribute), type); var factory = DataAnnotationsModelValidatorProvider.AttributeFactories.Values.Single(); var metadata = ModelMetadataProviders.Current.GetMetadataForType(() => null, typeof(object)); var context = new ControllerContext(); var attribute = new MyValidationAttribute(); var validator = factory(metadata, context, attribute); Assert.IsType <MyValidationAttributeAdapter>(validator); } finally { DataAnnotationsModelValidatorProvider.AttributeFactories = oldFactories; } }
public void Should_update_ErrorMessageResource_of_ValidationAttribute() { // Arrange var key = new ModelMetadataIdentity(); var myAttribute = new MyValidationAttribute(); var attributes = new ModelAttributes(new object[] { myAttribute }); var context = new ValidationMetadataProviderContext(key, attributes); var expectedErrorMessageResourceName = "SomeResourceName"; MyMockAdapter .Setup(x => x.CanHandle(myAttribute)) .Returns(true); MyMockAdapter .Setup(x => x.GetErrorMessageResourceName(myAttribute)) .Returns(expectedErrorMessageResourceName); // Act ProvierUnderTest.CreateValidationMetadata(context); // Assert Assert.Equal( expectedErrorMessageResourceName, myAttribute.ErrorMessageResourceName ); Assert.Equal( ProvierUnderTest.ErrorMessageResourceType, myAttribute.ErrorMessageResourceType ); }
public void Should_not_update_ErrorMessageResource_of_ValidationAttribute_with_custom_ErrorMessage() { // Arrange var key = new ModelMetadataIdentity(); var myAttribute = new MyValidationAttribute { ErrorMessage = "Some message" }; var attributes = new ModelAttributes(new object[] { myAttribute }); var context = new ValidationMetadataProviderContext(key, attributes); MyMockAdapter .Setup(x => x.CanHandle(myAttribute)) .Returns(true); MyMockAdapter .Setup(x => x.GetErrorMessageResourceName(myAttribute)) .Returns("Whatever") .Verifiable(); // Act ProvierUnderTest.CreateValidationMetadata(context); // Assert Assert.Null(myAttribute.ErrorMessageResourceName); Assert.Null(myAttribute.ErrorMessageResourceType); Assert.Equal("Some message", myAttribute.ErrorMessage); MyMockAdapter.Verify( x => x.GetErrorMessageResourceName(myAttribute), Times.Never ); }
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 Should_update_ErrorMessageResource_only_when_needed() { // Arrange var key = new ModelMetadataIdentity(); var myAttributeNotToUpdate = new MyValidationAttribute { ErrorMessage = "Some message" }; var myAttributeToUpdate = new MyValidationAttribute(); var someOtherNonValidationAttribute = new MyNonValidationAttribute(); var attributes = new ModelAttributes(new object[] { myAttributeNotToUpdate, myAttributeToUpdate, someOtherNonValidationAttribute }); var context = new ValidationMetadataProviderContext(key, attributes); var expectedErrorMessageResourceName = "SomeResourceName"; MyMockAdapter .Setup(x => x.CanHandle(myAttributeNotToUpdate)) .Returns(true) .Verifiable(); MyMockAdapter .Setup(x => x.GetErrorMessageResourceName(myAttributeNotToUpdate)) .Returns("Whatever") .Verifiable(); MyMockAdapter .Setup(x => x.CanHandle(myAttributeToUpdate)) .Returns(true); MyMockAdapter .Setup(x => x.GetErrorMessageResourceName(myAttributeToUpdate)) .Returns(expectedErrorMessageResourceName) .Verifiable(); // Act ProvierUnderTest.CreateValidationMetadata(context); // Assert Assert.Null(myAttributeNotToUpdate.ErrorMessageResourceName); Assert.Null(myAttributeNotToUpdate.ErrorMessageResourceType); Assert.Equal("Some message", myAttributeNotToUpdate.ErrorMessage); MyMockAdapter.Verify( x => x.CanHandle(myAttributeNotToUpdate), Times.Never ); MyMockAdapter.Verify( x => x.GetErrorMessageResourceName(myAttributeNotToUpdate), Times.Never ); Assert.Equal( expectedErrorMessageResourceName, myAttributeToUpdate.ErrorMessageResourceName ); Assert.Equal( ProvierUnderTest.ErrorMessageResourceType, myAttributeToUpdate.ErrorMessageResourceType ); MyMockAdapter.Verify( x => x.CanHandle(myAttributeToUpdate), Times.Once ); MyMockAdapter.Verify( x => x.GetErrorMessageResourceName(myAttributeToUpdate), Times.Once ); }