コード例 #1
0
        public static void CanRetrievePropertyValues()
        {
            const string lastName = "bar";
            const string firstName = "Foo";
            var instance = new MyTestType { FirstName = firstName, LastName = lastName };
            var propertyValues = DataAnnotationsValidator.GetPropertyValues(instance);

            Assert.Equal(firstName, propertyValues[nameof(instance.FirstName)]);
            Assert.Equal(lastName, propertyValues[nameof(instance.LastName)]);
        }
コード例 #2
0
        public static void CanGetAllPropertyNames()
        {
            var instance = new MyTestType();
            var publicProperties = DataAnnotationsValidator.GetAllPublicPropertyNamesToValidate(instance);
            var allNames = publicProperties.Select(p => p.Name).ToArray();

            Assert.Contains(nameof(instance.FirstName), allNames);
            Assert.Contains(nameof(instance.LastName), allNames);
            Assert.DoesNotContain("SecretCode", allNames);
        }
コード例 #3
0
        public void TestYourAssumptionWorks()
        {
            var instance = new MyTestType
            {
                FirstName = "a"
            };
            var context = new ValidationContext(instance)
            {
                MemberName = nameof(instance.FirstName)
            };
            var objectResults = new List<ValidationResult>();
            var isObjectValid = Validator.TryValidateObject(instance, context, objectResults);

            var propertyResults = new List<ValidationResult>();
            var isPropertyValid = Validator.TryValidateProperty(instance.FirstName, context, propertyResults);
        }
コード例 #4
0
 public void ValidateProperty(MyTestType toValidate,
     string propertyToValidate,
     Func<MyTestType, object> valueProvider,
     bool shouldBeValid)
 {
     var validator = new DataAnnotationsValidatorFixture().Build();
     var result = validator.ValidateProperty(toValidate, propertyToValidate, valueProvider(toValidate));
     if (shouldBeValid)
     {
         Assert.True(result.IsValid);
     }
     else
     {
         Assert.False(result.IsValid);
         Assert.True(result.ValidationResults.Any());
     }
 }