Exemplo n.º 1
0
        public void ValidateObject_Returns_Failed_Result_If_Any_Validator_Returns_Failed()
        {
            // Arrange
            var validator = new Validator();
            object target = new object();
            SetupValidators(validator, target, ValidationResult.Successful, ValidationResult.Successful, FailedResult, ValidationResult.Successful);

            // Act
            ValidationResult result = validator.ValidateObject(target);

            // Assert
            Assert.IsFalse(result.IsValid);
        }
Exemplo n.º 2
0
        public void ValidateObject_Returns_Successful_Result_If_All_Validators_Return_Successful()
        {
            // Arrange
            var validator = new Validator();
            object target = new object();
            SetupValidators(validator, target, ValidationResult.Successful, ValidationResult.Successful, ValidationResult.Successful);

            // Act
            ValidationResult result = validator.ValidateObject(target);

            // Assert
            Assert.IsTrue(result.IsValid);
        }
Exemplo n.º 3
0
        public void ValidateObject_Collects_Errors_From_All_Validators()
        {
            // Arrange
            var validator = new Validator();
            object target = new object();
            SetupValidators(validator, target, ValidationResult.Successful, FailedResult, AnotherFailedResult, ValidationResult.Successful);

            // Act
            ValidationResult result = validator.ValidateObject(target);

            // Assert
            Assert.IsFalse(result.IsValid);
            Assert.AreEqual(2, result.Errors.Count());
            EnumerableAssert.ElementsMatch(new[] {FailedResult, AnotherFailedResult}, result.Errors, (e, a) => ReferenceEquals(e.Errors.First(), a));
        }
Exemplo n.º 4
0
 private static void SetupValidators(Validator validator, object target, params ValidationResult[] validationResults)
 {
     validator.Validators.Clear();
     for (int i = 0; i < validationResults.Length; i++)
     {
         var mockValidator = new Mock<ObjectValidator>();
         mockValidator.Setup(v => v.ValidateObject(target)).Returns(validationResults[i]);
         validator.Validators.Add(mockValidator.Object);
     }
 }