Example #1
0
        public void SetUp()
        {
            this.mockery = new Mockery();

            // mock rule validationFactory
            this.validationFactory = this.mockery.NewMock<IValidationFactory>();
            IValidationResult aggregatedValidationResult = new ValidationResult(true);
            Stub.On(this.validationFactory).Method("CreateValidationResult").With(true).Will(
                Return.Value(aggregatedValidationResult));
            Stub.On(this.validationFactory).Method("CreateRuleSet").Will(Return.Value(new ValidationRuleSet()));

            // mock rules providers
            this.defaultRulesProvider = this.mockery.NewMock<IRulesProvider>();
            this.pluginRulesProvider = this.mockery.NewMock<IRulesProvider>();

            // mock rules provider finder
            this.rulesProviderFinder = this.mockery.NewMock<IRulesProviderFinder>();
            Stub.On(this.rulesProviderFinder).Method("FindRulesProviders").Will(
                Return.Value(new List<IRulesProvider> { this.defaultRulesProvider, this.pluginRulesProvider }));

            // create testee
            this.testee = new RuleEngine(this.rulesProviderFinder);
        }
 public void Creation()
 {
     this.validationResult = new ValidationResult(true);
 }
        public void StopOnFirstViolation()
        {
            ValidationAggregator testee = new ValidationAggregator(this.validationFactory, true);

            IRuleSet<IValidationRule> ruleSet = new ValidationRuleSet
                                                    {
                                                        this.mockery.NewMock<IValidationRule>(),
                                                        this.mockery.NewMock<IValidationRule>()
                                                    };

            Expect.On(ruleSet[0]).Method("Evaluate").Will(Return.Value(new ValidationResult(false)));

            ValidationResult invalidResult = new ValidationResult(false);
            invalidResult.Violations.Add(new ValidationViolation("test"));
            Expect.On(ruleSet[1]).Method("Evaluate").Will(Return.Value(invalidResult));
            
            string logInfo;
            IValidationResult result = testee.Aggregate(ruleSet, out logInfo);

            Assert.IsFalse(result.Valid, "aggregated result has to be valid if all rules are valid.");
            Assert.AreEqual(0, result.Violations.Count, "the violation of the second rule should not be in the result because aggregation should have stopped on first invalid rule.");
        }
        public void AggregateARuleSetWithInvalidRulesResultsInAnInvalidResult()
        {
            ValidationAggregator testee = new ValidationAggregator(this.validationFactory, false);

            IRuleSet<IValidationRule> ruleSet = new ValidationRuleSet
                                                    {
                                                        this.mockery.NewMock<IValidationRule>(),
                                                        this.mockery.NewMock<IValidationRule>()
                                                    };

            Expect.On(ruleSet[0]).Method("Evaluate").Will(Return.Value(new ValidationResult(true)));

            ValidationResult invalidResult = new ValidationResult(false);
            invalidResult.Violations.Add(new ValidationViolation("test"));
            Expect.On(ruleSet[1]).Method("Evaluate").Will(Return.Value(invalidResult));

            string logInfo;
            IValidationResult result = testee.Aggregate(ruleSet, out logInfo);

            Assert.IsFalse(result.Valid, "aggregated result has to be valid if all rules are valid.");
            Assert.AreEqual(1, result.Violations.Count, "violation was not passed to result.");
        }
        public void Creation()
        {
            ValidationResult validationResult = new ValidationResult(true);

            Assert.IsTrue(validationResult.Valid, "Validation result was created with a valid value equal to true.");
        }