コード例 #1
0
        public void DocBuilder_WithValidator_ReturnsRuleDescription()
        {
            var singleRuleValidator = new SingleRuleValidator();

            var expectedRuleDescription = new RuleDescriptor
            {
                MemberName = "First Name",
                Rules      = new List <RuleDescription>
                {
                    new RuleDescription
                    {
                        FailureSeverity   = "Error",
                        OnFailure         = "Continue",
                        ValidationMessage = "Mock message",
                        ValidatorName     = "NotEmptyValidator"
                    }
                }
            };

            var mockRuleDescriptor = new Mock <IRuleBuilder>();

            mockRuleDescriptor.Setup(x => x.BuildRuleDescription(It.IsAny <IEnumerable <IPropertyValidator> >(), It.IsAny <string>(),
                                                                 It.IsAny <CascadeMode>(), It.IsAny <PropertyRule>())).Returns(expectedRuleDescription);

            var docBuilder = new DocBuilder(mockRuleDescriptor.Object);

            var actualResult = docBuilder.Document(singleRuleValidator).ToList();

            actualResult.Should().HaveCount(1);
            actualResult.Should().BeEquivalentTo(expectedRuleDescription);
        }
コード例 #2
0
        public void ValiDoc_WithSingleRuleValidator_OutputSingleRule()
        {
            var validator = new SingleRuleValidator();

            var ruleGenerator = new DocBuilder(new RuleDescriptionBuilder(new ValidatorErrorMessageBuilder(new FluentValidationHelpers())));

            var validationRules = ruleGenerator.Document(validator).ToList();

            var expectedOutput = new List <RuleDescriptor>
            {
                new RuleDescriptor
                {
                    MemberName = "First Name",
                    Rules      = new List <RuleDescription>
                    {
                        new RuleDescription
                        {
                            FailureSeverity   = "Error",
                            OnFailure         = "Continue",
                            ValidatorName     = "NotEmptyValidator",
                            ValidationMessage = "'First Name' should not be empty."
                        }
                    }
                }
            };

            validationRules.Should().HaveCount(1);
            validationRules.ShouldBeEquivalentTo(expectedOutput, options => options.WithStrictOrdering());
        }
コード例 #3
0
        public void ValidationMessageBuilder_WithWhitespacePropertyName_ThrowsArgumentNullException()
        {
            var validator = new SingleRuleValidator();
            var validationErrorMessageBuilder = new ValidatorErrorMessageBuilder(new FluentValidationHelpers());

            var propertyRule = validator.CreateDescriptor().GetRulesForMember("FirstName").First() as PropertyRule;

            var expectedExcepton = Record.Exception(() => validationErrorMessageBuilder.GetErrorMessage(propertyRule.Validators.Single(), propertyRule, " "));

            expectedExcepton.Should().BeOfType <ArgumentNullException>();
            expectedExcepton.Message.Contains("propertyName");
        }
コード例 #4
0
        public void ValidationMessageBuilder_WithValidInput_BuildsValidationMessages()
        {
            const string expectedErrorMessage          = "'First Name' should not be empty.";
            var          validator                     = new SingleRuleValidator();
            var          validationErrorMessageBuilder = new ValidatorErrorMessageBuilder(new FluentValidationHelpers());

            var propertyRule = validator.CreateDescriptor().GetRulesForMember("FirstName").First() as PropertyRule;
            var propertyName = propertyRule.GetDisplayName();

            var actualErrorMessage = validationErrorMessageBuilder.GetErrorMessage(propertyRule.Validators.Single(), propertyRule, propertyName);

            actualErrorMessage.ShouldBeEquivalentTo(expectedErrorMessage);
        }
コード例 #5
0
        public void RuleDescriptionBuilder_WithSingleRuleValidator_ReturnsSingleRuleDescription()
        {
            const string      validationErrorMessage = "Mock message";
            const CascadeMode cascadeMode            = CascadeMode.Continue;

            var mockValidatorErrorMessageBuilder = new Mock <IValidatorErrorMessageBuilder>();

            mockValidatorErrorMessageBuilder.Setup(m => m.GetErrorMessage(It.IsAny <IPropertyValidator>(), It.IsAny <PropertyRule>(), It.IsAny <string>()))
            .Returns(validationErrorMessage);

            var validator    = new SingleRuleValidator();
            var propertyRule = validator.CreateDescriptor().GetRulesForMember("FirstName").First() as PropertyRule;
            var propertyName = propertyRule.GetDisplayName();

            var expectedRuleDescription = new RuleDescriptor
            {
                MemberName = "First Name",
                Rules      = new List <RuleDescription>
                {
                    new RuleDescription
                    {
                        FailureSeverity   = "Error",
                        OnFailure         = "Continue",
                        ValidationMessage = validationErrorMessage,
                        ValidatorName     = "NotEmptyValidator"
                    }
                }
            };

            var ruleDescriptionBuilder = new RuleDescriptionBuilder(mockValidatorErrorMessageBuilder.Object);

            var actualRuleDescription = ruleDescriptionBuilder.BuildRuleDescription(propertyRule.Validators, propertyName, cascadeMode, propertyRule);

            actualRuleDescription.Rules.Should().HaveCount(1);
            actualRuleDescription.ShouldBeEquivalentTo(expectedRuleDescription);
        }