コード例 #1
0
        public void ValidateCorrectlyWhenUnderMaxLength()
        {
            //--Arrange
            var model = new ModelWithMaxLengthFieldWithoutCustomErrorMessage
            {
                MultipleWordedField = "1"
            };
            var validationContext = new ValidationContext(model, null, null);
            var result            = new List <ValidationResult>();

            //--Act
            var valid = Validator.TryValidateObject(model, validationContext, result, true);

            //--Assert
            valid.ShouldBeTrue();
            result.Count.ShouldBe(0);
        }
コード例 #2
0
        public void InvalidateCorrectlyWithoutCustomErrorMessage()
        {
            //--Arrange
            var model = new ModelWithMaxLengthFieldWithoutCustomErrorMessage
            {
                MultipleWordedField = "123"
            };
            var validationContext = new ValidationContext(model, null, null);
            var result            = new List <ValidationResult>();

            FormatResponseMessage.SetSnakeCaseSetting(false);

            //--Act
            var valid = Validator.TryValidateObject(model, validationContext, result, true);

            //--Assert
            valid.ShouldBeFalse();
            result.Count.ShouldBe(1);
            const string defaultErrorMsg = "The 'MultipleWordedField' field cannot be greater than '2' characters long.";

            result[0].ErrorMessage
            .ShouldBe(ModelStateCustomErrorMessage.Create(1, defaultErrorMsg));
        }