コード例 #1
0
        public void CanASpecificationDetermineNotSatisfied()
        {
            var testSubject = new StringTestSubject();
            var spec        = new Specification <StringTestSubject>(p => !String.IsNullOrEmpty(p.TestString1));

            Assert.IsFalse(spec.IsSatisfiedBy(testSubject));
        }
コード例 #2
0
        public void CanAValidationRuleBeCreatedViaLambdaWithMessageLambda()
        {
            var testSubject = new StringTestSubject();
            var rule        = new ValidationRule <StringTestSubject>(x => !String.IsNullOrEmpty(x.TestString1), "Rule", x => String.Format("Entity of type {0} first name is null.", x.GetType().Name));
            var valid       = rule.Validate(testSubject);

            Assert.AreEqual("Entity of type StringTestSubject first name is null.", rule.ErrorMessage);
        }
コード例 #3
0
        public void CanSpecificationBeNOTedByOperator()
        {
            var testSubject = new StringTestSubject {
                TestString1 = "Bob"
            };
            var spec = !(new Specification <StringTestSubject>(p => !String.IsNullOrEmpty(p.TestString1)));

            Assert.IsFalse(spec.IsSatisfiedBy(testSubject));
        }
コード例 #4
0
        public void CanASpecificationDetermineSatisfaction()
        {
            var testSubject = new StringTestSubject {
                TestString1 = "Bob"
            };
            var spec = new Specification <StringTestSubject>(p => !String.IsNullOrEmpty(p.TestString1));


            Assert.IsTrue(spec.IsSatisfiedBy(testSubject));
        }
コード例 #5
0
        public void InvalidEntityValidatesBySpecificationAsInvalid()
        {
            var ruleSet = new StringTestSubjectRules();

            ruleSet.AddRule(x => !String.IsNullOrEmpty(x.TestString1), "TestString1Required.");
            var testSubject = new StringTestSubject();

            var result = ruleSet.Validate(testSubject);

            Assert.IsFalse(result.Valid);
        }
コード例 #6
0
        public void ValidEntityValidatesBySpecificationAsValid()
        {
            var ruleSet     = new StringTestSubjectRules();
            var testSubject = new StringTestSubject {
                TestString1 = "Bob"
            };

            var result = ruleSet.Validate(testSubject);

            Assert.IsTrue(result.Valid);
        }
コード例 #7
0
        public void CanValidateEntitySpecifications()
        {
            var ruleSet     = new StringTestSubjectRules();
            var testSubject = new StringTestSubject {
                TestString1 = "Bob"
            };

            var result = ruleSet.Validate(testSubject);

            Assert.IsNotNull(result);
        }
コード例 #8
0
        public void ValidationResultContainsPointerToEntityThatFailedValidation()
        {
            var ruleSet = new StringTestSubjectRules();

            ruleSet.AddRule(x => !String.IsNullOrEmpty(x.TestString1), "TestString1Required.");
            var testSubject = new StringTestSubject {
                TestString1 = "Bob"
            };

            var result = ruleSet.Validate(testSubject);

            Assert.AreEqual(testSubject, result.Entity);
        }
コード例 #9
0
        public void ValidatedEntityReportsErrors()
        {
            var ruleSet = new StringTestSubjectRules();

            ruleSet.AddRule(x => !String.IsNullOrEmpty(x.TestString2), "TestString2Required.");
            var testSubject = new StringTestSubject {
                TestString1 = "Bob"
            };

            ruleSet.Validate(testSubject);

            Assert.AreEqual(1, testSubject.ValidationErrors.Count);
        }
コード例 #10
0
        public void ValidatedEntityCanGetErrorsReported()
        {
            var ruleSet = new StringTestSubjectRules();

            ruleSet.AddRule(x => !String.IsNullOrEmpty(x.TestString1), "TestString1Required.");
            var testSubject = new StringTestSubject {
                TestString1 = "Bob"
            };

            ruleSet.Validate(testSubject);

            Assert.IsNotNull(testSubject.ValidationErrors);
        }
コード例 #11
0
        public void CanSpecificationsBeANDedTogether()
        {
            var testSubject = new StringTestSubject {
                TestString1 = "Bob"
            };
            var spec = new Specification <StringTestSubject>(p => !String.IsNullOrEmpty(p.TestString1)).And(
                new Specification <StringTestSubject>(p => !String.IsNullOrEmpty(p.TestString2)));

            Assert.IsFalse(spec.IsSatisfiedBy(testSubject));

            testSubject.TestString2 = "Smith";
            Assert.IsTrue(spec.IsSatisfiedBy(testSubject));
        }
コード例 #12
0
        public void CanSpecificationBeORedByOperator()
        {
            var testSubject = new StringTestSubject();
            var spec        = new Specification <StringTestSubject>(p => !String.IsNullOrEmpty(p.TestString1)) |
                              new Specification <StringTestSubject>(p => !String.IsNullOrEmpty(p.TestString2));

            Assert.IsFalse(spec.IsSatisfiedBy(testSubject));

            testSubject.TestString2 = "Smith";
            Assert.IsTrue(spec.IsSatisfiedBy(testSubject));

            testSubject.TestString1 = "Bob";
            testSubject.TestString2 = null;
            Assert.IsTrue(spec.IsSatisfiedBy(testSubject));
        }
コード例 #13
0
        public void DoesInvalidationSetDefaultErrorMessage()
        {
            var testSubject = new StringTestSubject {
                TestString1 = "Bob"
            };
            var spec = new Specification <StringTestSubject>(p => !String.IsNullOrEmpty(p.TestString1));

            spec &= new Specification <StringTestSubject>(p => !String.IsNullOrEmpty(p.TestString2));

            var rule = new ValidationRule <StringTestSubject>(spec, "FullNameRequired");

            var result = rule.Validate(testSubject);

            Assert.IsNotNullOrEmpty(rule.ErrorMessage);
        }
コード例 #14
0
        public void CanAnEntityBeInvalidated()
        {
            var testSubject = new StringTestSubject {
                TestString1 = "Bob"
            };
            var spec = new Specification <StringTestSubject>(p => !String.IsNullOrEmpty(p.TestString1));

            spec &= new Specification <StringTestSubject>(p => !String.IsNullOrEmpty(p.TestString2));

            var rule = new ValidationRule <StringTestSubject>(spec, "FullNameRequired");

            var result = rule.Validate(testSubject);

            Assert.IsFalse(result);
        }