示例#1
0
        public void IsValid_WhenStringLength_IsLessThanMaxLength()
        {
            var command = new FakeStringLengthCommand {
                StringProperty = Guid.NewGuid().ToString()
            };
            var validator = new FakeStringMaxLengthValidator(command.StringProperty.Length + 1);

            var result = validator.Validate(command);

            result.IsValid.ShouldBeTrue();
            Func <ValidationFailure, bool> maxLengthError = x => x.PropertyName == command.PropertyName(y => y.StringProperty);

            result.Errors.Any(maxLengthError).ShouldBeFalse();
            validator.ShouldNotHaveValidationErrorFor(x => x.StringProperty, command.StringProperty);
        }
示例#2
0
        public void IsValid_WhenString_IsNull()
        {
            var command = new FakeStringLengthCommand {
                StringProperty = null
            };
            var validator = new FakeStringMaxLengthValidator(5);

            var result = validator.Validate(command);

            result.IsValid.ShouldBeTrue();
            Func <ValidationFailure, bool> maxLengthError = x => x.PropertyName == command.PropertyName(y => y.StringProperty);

            result.Errors.Any(maxLengthError).ShouldBeFalse();
            validator.ShouldNotHaveValidationErrorFor(x => x.StringProperty, command.StringProperty);
        }
示例#3
0
        public void IsInvalid_WhenStringLength_IsGreaterThanMaxLength()
        {
            var command = new FakeStringLengthCommand {
                StringProperty = "asdf"
            };
            var validator = new FakeStringMaxLengthValidator(3);

            var result = validator.Validate(command);

            result.IsValid.ShouldBeFalse();
            Func <ValidationFailure, bool> maxLengthError = x => x.PropertyName == command.PropertyName(y => y.StringProperty);

            result.Errors.Count(maxLengthError).ShouldEqual(1);
            result.Errors.Single(maxLengthError).ErrorMessage.ShouldEqual(Resources.Validation_MaxLength
                                                                          .Replace("{PropertyName}", "String Property")
                                                                          .Replace("{MaxLength}", "3")
                                                                          .Replace("{TotalLength}", command.StringProperty.Length.ToString(CultureInfo.InvariantCulture))
                                                                          );
            validator.ShouldHaveValidationErrorFor(x => x.StringProperty, command.StringProperty);
        }