Пример #1
0
        public void TestMessageForSpecificRule()
        {
            var builder = new Fluent.FluentBuilder();
            builder.For<MyMessageTestClass>()
                .Setup(m => m.A)
                    .MustBeLessThanOrEqualTo(1)
                    .WithMessage("Must be Less Than or equal to {0}");

            var engine = builder.Build();
            var report = new TestingValidationReport(engine);
            var obj = new MyMessageTestClass(2, 2);
            report.Validate(obj);
            report.AssertError(obj, o => o.A, RuleKinds.LessThanOrEqualToRule, 1);
        }
Пример #2
0
        public void ShouldBlameExplicitlySelectedCulprit_BlameIsAfterTheRule()
        {
            var builder = new Fluent.FluentBuilder();
            builder.For<Person>()
                    .Setup(p => p.FirstName)
                        .MustBeOneOf("John", "Paul", "James")
                        .Blame(p => p.HomeAddress, a => a.Line1);

            var engine = builder.Build();
            var report = new TestingValidationReport(engine);
            var address = MakeAddress("18 Perkins St", null, "1234", "Brisbane");
            report.Validate(MakePerson("Jane", "Holland", address));
            report.AssertError(address, m => m.Line1, RuleKinds.OneOfRule, null);
        }
Пример #3
0
        public void SimpleInterfaceTest()
        {
            var builder = new Fluent.FluentBuilder();
            builder.For<IMyClass>()
                    .Setup(m => m.A)
                        .MustBeGreaterThan(0);
            
            var engine = builder.Build();
            var report = new TestingValidationReport(engine);

            var o1 = new MyClass(0);
            Assert.IsFalse(report.Validate(o1));
            Assert.AreEqual(1, report.Errors.Length);
            report.AssertError<IMyClass, int>(o1, p1 => p1.A, RuleKinds.GreaterThanRule, 0);

            var o2 = new MyClassExplicit(0);
            Assert.IsFalse(report.Validate(o2));
            Assert.AreEqual(1, report.Errors.Length);
            report.AssertError<IMyClass, int>(o2, p1 => p1.A, RuleKinds.GreaterThanRule, 0);
        }
Пример #4
0
        //TODO: Blame Culprit when using composition!
        //[TestMethod]
        public void ShouldBlameExplicitlySelectedCulprit_Composition()
        {
            var builder = new Fluent.FluentBuilder();
            builder.For<Person>()
                    .Setup(p => p.HomeAddress)
                        .CallValidate()
                        .Blame(p => p.FirstName);

            builder.For<Address>()
                    .Setup(a => a.Line1)
                        .MustEqual("Line1");
                    


            var engine = builder.Build();
            var report = new TestingValidationReport(engine);
            var address = MakeAddress("Not Line 1", null, "1234", "Brisbane");
            var person = MakePerson("Blinky", "Bill", address);
            report.Validate(person);
            report.AssertError(person, m => m.FirstName, RuleKinds.EqualRule, "Line1");
        }
Пример #5
0
        public void ShouldBlameExplicitlySelectedCulprit_DifferentCulpritForDifferentRules()
        {
            var builder = new Fluent.FluentBuilder();
            builder.For<Person>()
                    .Setup(p => p.FirstName)
                        .MustNotEqual("Paul")
                        .Blame(p => p.HomeAddress, a => a.Line1)
                        .MustNotEqual("Peter")
                        .Blame(p => p.HomeAddress, a => a.PostCode);

            var engine = builder.Build();
            var report = new TestingValidationReport(engine);
            var address = MakeAddress("18 Perkins St", null, "1234", "Brisbane");
            report.Validate(MakePerson("Paul", "Holland", address));
            report.AssertError(address, m => m.Line1, RuleKinds.NotEqualRule, null);

            report.Clear();
            report.Validate(MakePerson("Peter", "Holland", address));
            report.AssertError(address, m => m.PostCode, RuleKinds.NotEqualRule, null);

        }