public void DomainEntityCan_ValidateChildDomainEntity() { var domainEntity = new MockValidatableDomainEntity(); var child = new MockChildDomainEntity { ValueFour = 5 }; domainEntity.SetChild(child); var valResults = Validate(domainEntity); valResults.ValidationType.Should().Be(ValidationTypes.Error); // The validation result aggregate should be the domain entity that was validated. valResults.RootObject.Should().BeSameAs(domainEntity); // Assert the child object validations. var childObjVal = valResults.ObjectValidations.SingleOrDefault(); childObjVal.Should().NotBeNull(); childObjVal.Object.Should().BeSameAs(child); // Assert the single child entity validation. var valItem = childObjVal.Validations.SingleOrDefault(); valItem.Should().NotBeNull(); valItem.Message.Should().Be("ValueFourValidationMessage"); }
public void DomainEntityValidated_UsingMsValidationAttributes_ByDefault() { var domainEntity = new MockValidatableDomainEntity { ValueOne = new string('X', 50), ValueTwo = 200 }; var valResult = Validate(domainEntity); valResult.ValidationType.Should().Be(ValidationTypes.Error); // There should be one domain entity validation with two validations items // the above two specified properties are out of range. valResult.ObjectValidations.Should().HaveCount(1); var entityValidation = valResult.ObjectValidations.First(); entityValidation.Should().NotBeNull(); entityValidation.Validations.Should().HaveCount(2); // The entity validation has reference to invalid domain entity. entityValidation.Object.Should().BeSameAs(domainEntity); // Validate the messages for each validation message. entityValidation.Validations[0].Message.Should().Be("ValueOneValidationMessage"); entityValidation.Validations[1].Message.Should().Be("ValueTwoValidationMessage"); // The validation result has reference to the root domain entity that was validated. valResult.RootObject.Should().BeSameAs(domainEntity); }
public void DomainEntityCan_ValidateChildDomainEntities() { var domainEntity = new MockValidatableDomainEntity(); var children = new[] { new MockChildDomainEntity { ValueFour = 5 }, new MockChildDomainEntity { ValueFour = 8 } }; domainEntity.SetChildren(children); var valResults = Validate(domainEntity); valResults.ValidationType.Should().Be(ValidationTypes.Error); // The validation result aggregate should be the domain entity that was validated. valResults.RootObject.Should().BeSameAs(domainEntity); // Assert the child object validations. valResults.ObjectValidations.Should().HaveCount(2); }
public void CanThrowExceptionFor_InvalidResult() { var domainEntity = new MockValidatableDomainEntity { ValueOne = new string('X', 50) }; var valResult = Validate(domainEntity); Assert.ThrowsAny <ValidationResultException>(() => valResult.ThrowIfInvalid()); }
public void IfValidatorDetermines_EntityNotValid_EntityValidationMethodNotInvoked() { var domainEntity = new MockValidatableDomainEntity { ValueOne = new string('X', 50), ValueTwo = 200 }; var valResult = Validate(domainEntity); valResult.ValidationType.Should().Be(ValidationTypes.Error); domainEntity.EntityValidateInvoked.Should().BeFalse(); }
public void ResultValidationLevel_IsMaxValidationLevel_ForAllInvalidations() { var domainEntity = new MockValidatableDomainEntity(); var child = new MockChildDomainEntity(); domainEntity.ValueInfoOne = 33; child.ValueWarningTwo = 77; domainEntity.SetChild(child); var valResult = Validate(domainEntity); valResult.ValidationType.Should().Be(ValidationTypes.Warning); }
public void DomainEntityCalled_DuringValidation() { var domainEntity = new MockValidatableDomainEntity { ValueThree = 200 }; var valResult = Validate(domainEntity); valResult.ValidationType.Should().Be(ValidationTypes.Error); valResult.ObjectValidations.First() .Validations.First() .Message.Should().Be("ValueThreeValidationMessage"); }
public void ResultIsFlatenedList_OfAllValidatedEntities() { var domainEntity = new MockValidatableDomainEntity(); var child = new MockChildDomainEntity(); domainEntity.ValueInfoOne = 33; child.ValueWarningTwo = 77; domainEntity.SetChild(child); var valResult = Validate(domainEntity); valResult.ObjectValidations.SelectMany(ov => ov.Validations) .Should().HaveCount(2) .And.Contain(v => v.ValidationType == ValidationTypes.Info) .And.Contain(v => v.ValidationType == ValidationTypes.Warning); }