Exemple #1
0
            public void ShouldValidateComplexObjectWithMultipleValidators()
            {
                // Arrange
                var value = new Staff
                {
                    Name        = "Joe Bloggs",
                    JobTitle    = null,                      // Invalid job title
                    Department  = "Build",
                    DateOfBirth = DateTime.UtcNow.AddDays(1) // Invalid birth date
                };

                var collection = new FluentValidatorCollection <Person> {
                    new PersonValidator(), new StaffValidator()
                };

                // Act
                collection.Validate(value);

                // Assert
                collection.FeedbackMessages.ShouldNotBeEmpty();
                collection.FeedbackMessages.Count().ShouldBe(2);
                collection.FeedbackMessages.FirstOrDefault(x => x.Equals(PersonValidator.DateOfBirthValidationMessage,
                                                                         StringComparison.InvariantCultureIgnoreCase)).ShouldNotBeNull();
                collection.FeedbackMessages.FirstOrDefault(x => x.Equals(StaffValidator.JobTitleValidationMessage,
                                                                         StringComparison.InvariantCultureIgnoreCase)).ShouldNotBeNull();
            }
Exemple #2
0
            public void ShouldBeEmptyIfDefaultConstructor()
            {
                // Act
                var collection = new FluentValidatorCollection <Person>();

                // Assert
                collection.Count.ShouldBe(0);
            }
Exemple #3
0
            public void ShouldAddSingleItem()
            {
                // Arrange
                var objectToAdd = new PersonValidator();

                // Act
                var collection = new FluentValidatorCollection <Person> {
                    objectToAdd
                };

                // Assert
                collection.ShouldContain(objectToAdd);
            }
Exemple #4
0
            public void ShouldBeDirtyOnceValidated()
            {
                // Arrange
                var value = new Person();

                var collection = new FluentValidatorCollection <Person> {
                    new PersonValidator()
                };

                // Act
                collection.Validate(value);

                // Assert
                collection.IsDirty.ShouldBe(true);
            }
Exemple #5
0
            public void ShouldContainItemsIfInitializedAsEnumerable()
            {
                // Arrange
                IEnumerable <AbstractValidator <Person> > validators = new List <AbstractValidator <Person> >
                {
                    new PersonValidator()
                };

                // Act
                var collection = new FluentValidatorCollection <Person>(validators);

                // Assert
                collection.Count.ShouldBe(validators.Count());
                collection.ToList().ShouldBeEquivalentTo(validators);
            }
Exemple #6
0
            public void ShouldBeInvalidIfInvalidValue()
            {
                // Arrange
                var value = new Person();

                var collection = new FluentValidatorCollection <Person> {
                    new PersonValidator()
                };

                // Act
                collection.Validate(value);

                // Assert
                collection.IsInvalid.ShouldBe(true);
            }
Exemple #7
0
            public void ShouldBeValidIfValidValue()
            {
                // Arrange
                var value = new Person {
                    Name = "Joe Bloggs", DateOfBirth = new DateTime(1992, 01, 01)
                };

                var collection = new FluentValidatorCollection <Person> {
                    new PersonValidator()
                };

                // Act
                collection.Validate(value);

                // Assert
                collection.IsInvalid.ShouldBe(false);
            }
Exemple #8
0
            public void ShouldAddRangeOfItems()
            {
                // Arrange
                IEnumerable <AbstractValidator <Person> > validators = new List <AbstractValidator <Person> >
                {
                    new PersonValidator()
                };

                var collection = new FluentValidatorCollection <Person>();

                // Act
                collection.AddRange(validators);

                // Assert
                foreach (AbstractValidator <Person> item in validators)
                {
                    collection.ShouldContain(item);
                }
            }
Exemple #9
0
            public void ShouldHaveFeedbackMessagesIfInvalidValue()
            {
                // Arrange
                var value = new Person
                {
                    Name        = "Joe Bloggs",
                    DateOfBirth = DateTime.UtcNow.AddDays(1) // Invalid birth date
                };

                var collection = new FluentValidatorCollection <Person> {
                    new PersonValidator()
                };

                // Act
                collection.Validate(value);

                // Assert
                collection.FeedbackMessages.ShouldNotBeEmpty();
                collection.FeedbackMessages.Count().ShouldBe(1);
                collection.FeedbackMessages.FirstOrDefault().ShouldBe(PersonValidator.DateOfBirthValidationMessage);
            }