public void ValidateMustReturnInvalidValidationResultWhenTheSpecifiedSpecificationIsSatisfiedAndTheValidatorReturnsFalse()
        {
            const string firstName = "Foo";

            IValidator validator = Substitute.For <IValidator>();

            validator.IsValid(null).ReturnsForAnyArgs(false);

            ISpecification <Customer> specification = Substitute.For <ISpecification <Customer> >();

            specification.IsSatisfiedBy(null).ReturnsForAnyArgs(true);

            EntityPropertyValidationRule <Customer, string> entityPropertyValidationRule = new EntityPropertyValidationRule <Customer, string>(new EntityPropertyValidator(validator), Substitute.For <IPropertyDisplayNameResolver>(), x => x.FirstName, specification);
            ValidationResult validationResult = entityPropertyValidationRule.Validate(new Customer
            {
                FirstName = firstName
            });

            validator.Received(1).IsValid(firstName);
            specification.ReceivedWithAnyArgs(1).IsSatisfiedBy(null);

            Assert.AreEqual(1, validationResult.Errors.Count);
            Assert.AreEqual(LinqUtils.GetMemberName <Customer, string>(x => x.FirstName), validationResult.Errors[0].PropertyName);
            Assert.IsFalse(validationResult.IsValid);
        }
        public void ValidateEnsureValidatorIsValidIsCalledForTheEntityProperty()
        {
            const string firstName = "Foo";

            IValidator validator = Substitute.For<IValidator>();
            EntityPropertyValidationRule<Customer, string> entityPropertyValidationRule = new EntityPropertyValidationRule<Customer, string>(new EntityPropertyValidator(validator), Substitute.For<IPropertyDisplayNameResolver>(), x => x.FirstName);
            entityPropertyValidationRule.Validate(new Customer
                                                      {
                                                          FirstName = firstName
                                                      });

            validator.Received(1).IsValid(firstName);
        }
        public void ValidateEnsureValidatorIsValidIsCalledForTheEntityProperty()
        {
            const string firstName = "Foo";

            IValidator validator = Substitute.For <IValidator>();
            EntityPropertyValidationRule <Customer, string> entityPropertyValidationRule = new EntityPropertyValidationRule <Customer, string>(new EntityPropertyValidator(validator), Substitute.For <IPropertyDisplayNameResolver>(), x => x.FirstName);

            entityPropertyValidationRule.Validate(new Customer
            {
                FirstName = firstName
            });

            validator.Received(1).IsValid(firstName);
        }
        public void ValidateMustReturnValidWhenValidatorIsValidReturnsTrue()
        {
            const string firstName = "Foo";

            IValidator validator = Substitute.For <IValidator>();

            validator.IsValid(firstName).Returns(true);

            EntityPropertyValidationRule <Customer, string> entityPropertyValidationRule = new EntityPropertyValidationRule <Customer, string>(new EntityPropertyValidator(validator), Substitute.For <IPropertyDisplayNameResolver>(), x => x.FirstName);
            ValidationResult validationResult = entityPropertyValidationRule.Validate(new Customer
            {
                FirstName = firstName
            });

            validator.Received(1).IsValid(firstName);

            Assert.AreEqual(0, validationResult.Errors.Count);
            Assert.IsTrue(validationResult.IsValid);
        }
        public void ValidateMustReturnEmptyValidationResultWhenTheSpecifiedSpecificationIsNotSatisfied()
        {
            const string firstName = "Foo";

            IValidator validator = Substitute.For <IValidator>();
            ISpecification <Customer> specification = Substitute.For <ISpecification <Customer> >();

            specification.IsSatisfiedBy(null).ReturnsForAnyArgs(false);

            EntityPropertyValidationRule <Customer, string> entityPropertyValidationRule = new EntityPropertyValidationRule <Customer, string>(new EntityPropertyValidator(validator), Substitute.For <IPropertyDisplayNameResolver>(), x => x.FirstName, specification);
            ValidationResult validationResult = entityPropertyValidationRule.Validate(new Customer
            {
                FirstName = firstName
            });

            validator.DidNotReceiveWithAnyArgs().IsValid(firstName);
            specification.ReceivedWithAnyArgs(1).IsSatisfiedBy(null);

            Assert.AreEqual(0, validationResult.Errors.Count);
            Assert.IsTrue(validationResult.IsValid);
        }
        public void ValidateMustReturnValidWhenValidatorIsValidReturnsTrue()
        {
            const string firstName = "Foo";

            IValidator validator = Substitute.For<IValidator>();
            validator.IsValid(firstName).Returns(true);

            EntityPropertyValidationRule<Customer, string> entityPropertyValidationRule = new EntityPropertyValidationRule<Customer, string>(new EntityPropertyValidator(validator), Substitute.For<IPropertyDisplayNameResolver>(), x => x.FirstName);
            ValidationResult validationResult = entityPropertyValidationRule.Validate(new Customer
                                                                                          {
                                                                                              FirstName = firstName
                                                                                          });

            validator.Received(1).IsValid(firstName);

            Assert.AreEqual(0, validationResult.Errors.Count);
            Assert.IsTrue(validationResult.IsValid);
        }
        public void ValidateMustReturnInvalidValidationResultWhenTheSpecifiedTheValidatorReturnsFalse()
        {
            const string firstName = "Foo";

            IValidator validator = Substitute.For<IValidator>();
            validator.IsValid(null).ReturnsForAnyArgs(false);

            EntityPropertyValidationRule<Customer, string> entityPropertyValidationRule = new EntityPropertyValidationRule<Customer, string>(new EntityPropertyValidator(validator), Substitute.For<IPropertyDisplayNameResolver>(), x => x.FirstName);
            ValidationResult validationResult = entityPropertyValidationRule.Validate(new Customer
            {
                FirstName = firstName
            });

            validator.Received(1).IsValid(firstName);

            Assert.AreEqual(1, validationResult.Errors.Count);
            Assert.AreEqual(LinqUtils.GetMemberName<Customer, string>(x => x.FirstName), validationResult.Errors[0].PropertyName);
            Assert.IsFalse(validationResult.IsValid);
        }
        public void ValidateMustReturnEmptyValidationResultWhenTheSpecifiedSpecificationIsSatisfiedAndTheValidatorReturnsTrue()
        {
            const string firstName = "Foo";

            IValidator validator = Substitute.For<IValidator>();
            validator.IsValid(null).ReturnsForAnyArgs(true);

            ISpecification<Customer> specification = Substitute.For<ISpecification<Customer>>();
            specification.IsSatisfiedBy(null).ReturnsForAnyArgs(true);

            EntityPropertyValidationRule<Customer, string> entityPropertyValidationRule = new EntityPropertyValidationRule<Customer, string>(new EntityPropertyValidator(validator), Substitute.For<IPropertyDisplayNameResolver>(), x => x.FirstName, specification);
            ValidationResult validationResult = entityPropertyValidationRule.Validate(new Customer
            {
                FirstName = firstName
            });

            validator.Received(1).IsValid(firstName);
            specification.ReceivedWithAnyArgs(1).IsSatisfiedBy(null);

            Assert.AreEqual(0, validationResult.Errors.Count);
            Assert.IsTrue(validationResult.IsValid);
        }