Example #1
0
        public void InvalidIfValueIsNullOrEmptyString()
        {
            var model = new Mock {
                Property = "value"
            };
            var context = new ValidationContext(model);

            var attr = new RequiredIfAttribute("Property", "value");

            var result = attr.GetValidationResult(" ", context);

            Assert.Equal("'Mock' is required because 'Property' has a value 'value'.", result.ErrorMessage);

            result = attr.GetValidationResult(null, context);

            Assert.Equal("'Mock' is required because 'Property' has a value 'value'.", result.ErrorMessage);
        }
Example #2
0
        public void ShallThrowIfValidationContextIsNull()
        {
            var attr = new RequiredIfAttribute("test", "value");

            Assert.Throws <ArgumentNullException>(() =>
            {
                attr.GetValidationResult("value", null);
            });
        }
Example #3
0
        public void SuccessfulValidationWithoutTargetedValue()
        {
            var model = new Mock {
                Property = "not the expected target"
            };
            var context = new ValidationContext(model);

            var attr = new RequiredIfAttribute("Property", "value");

            var result = attr.GetValidationResult("value", context);

            Assert.Equal(ValidationResult.Success, result);
        }
Example #4
0
        public void ShallThrowIfPropertyIsNotFound()
        {
            var model = new Mock {
                Property = null
            };
            var context = new ValidationContext(model);

            var attr = new RequiredIfAttribute("UnknownProperty", "value");

            Assert.Throws <ArgumentNullException>(() =>
            {
                attr.GetValidationResult("value", context);
            });
        }