public void CreateContextShouldNotAllowNullInstance() { // arrange object instance = null; var validator = new Validator(); // act var ex = Assert.Throws<ArgumentNullException>( () => validator.CreateContext( instance, null ) ); // assert Assert.Equal( "instance", ex.ParamName ); }
public void TryValidatePropertyShouldReturnTrueForTypeWithoutRules() { // arrange var instance = new Mock<IComponent>().Object; var validator = new Validator(); var context = validator.CreateContext( instance, null ); var results = new List<IValidationResult>(); var value = instance.Site; // act var valid = validator.TryValidateProperty( value, context, results ); // assert Assert.True( valid ); Assert.Equal( 0, results.Count ); }
public void ValidatePropertyShouldThrowExceptionForInvalidValue() { // arrange var instance = new Mock<IComponent>().Object; var validator = new Validator(); var context = validator.CreateContext( instance, null ); var results = new List<IValidationResult>(); var value = instance.Site; validator.For<IComponent>().Property( c => c.Site ).Required(); context.MemberName = "Site"; // act var ex = Assert.Throws<ValidationException>( () => validator.ValidateProperty( value, context ) ); // assert Assert.Equal( "Site", ex.ValidationResult.MemberNames.First() ); Assert.Equal( value, ex.Value ); }
public void TryValidateObjectShouldReturnTrueForNullInstance() { // arrange IComponent instance = null; var validator = new Validator(); var context = validator.CreateContext( new Mock<IComponent>().Object, null ); var results = new List<IValidationResult>(); validator.For<IComponent>().Property( c => c.Site ).Required(); // act var valid = validator.TryValidateObject( instance, context, results ); // assert Assert.True( valid ); Assert.Equal( 0, results.Count ); }
public void TryValidatePropertyShouldReturnExpectedResult( IComponent instance, int resultCount, bool expected ) { // arrange var validator = new Validator(); var context = validator.CreateContext( instance, null ); var results = new List<IValidationResult>(); var value = instance.Site; validator.For<IComponent>().Property( c => c.Site ).Required(); context.MemberName = "Site"; // act var actual = validator.TryValidateProperty( value, context, results ); // assert Assert.Equal( expected, actual ); Assert.Equal( resultCount, results.Count ); }
public void GreaterThanOrEqualToShouldEvaluateWithCustomErrorMessage() { // arrange var validator = new Validator(); var instance = new StubModel() { StartDate = DateTime.Today, EndDate = DateTime.Today.AddDays( -1 ) }; var context = validator.CreateContext( instance, null ); var results = new List<IValidationResult>(); var expected = "Invalid"; validator.For<StubModel>().Property( m => m.EndDate ).GreaterThanOrEqualTo( m => m.StartDate, expected ); // act var valid = validator.TryValidateObject( instance, context, results ); // assert Assert.False( valid ); Assert.Equal( 1, results.Count ); Assert.Equal( expected, results[0].ErrorMessage ); Assert.True( results[0].MemberNames.SequenceEqual( new[] { "EndDate", "StartDate" } ) ); }
public void GreaterThanOrEqualToShouldEvaluateSuccess() { // arrange var validator = new Validator(); var instance = new StubModel(); var context = validator.CreateContext( instance, null ); var results = new List<IValidationResult>(); validator.For<StubModel>().Property( m => m.EndDate ).GreaterThanOrEqualTo( m => m.StartDate ); // act var valid = validator.TryValidateObject( instance, context, results ); // assert Assert.True( valid ); Assert.Equal( 0, results.Count ); }
public void GreaterThanShouldEvaluateExpectedResult() { // arrange var validator = new Validator(); var instance = new StubModel(); var context = validator.CreateContext( instance, null ); var results = new List<IValidationResult>(); validator.For<StubModel>().Property( m => m.EndDate ).GreaterThan( m => m.StartDate ); // act var valid = validator.TryValidateObject( instance, context, results ); // assert Assert.False( valid ); Assert.Equal( 1, results.Count ); Assert.Equal( "The EndDate field must be greater than the StartDate field.", results[0].ErrorMessage ); Assert.True( results[0].MemberNames.SequenceEqual( new[] { "EndDate", "StartDate" } ) ); }
public void LessThanOrEqualToShouldEvaluateExpectedResult() { // arrange var validator = new Validator(); var instance = new StubModel() { StartDate = DateTime.Today, EndDate = DateTime.Today.AddDays( -1 ) }; var context = validator.CreateContext( instance, null ); var results = new List<IValidationResult>(); validator.For<StubModel>().Property( m => m.StartDate ).LessThanOrEqualTo( m => m.EndDate ); // act var valid = validator.TryValidateObject( instance, context, results ); // assert Assert.False( valid ); Assert.Equal( 1, results.Count ); Assert.Equal( "The StartDate field must be less than or equal to the EndDate field.", results[0].ErrorMessage ); Assert.True( results[0].MemberNames.SequenceEqual( new[] { "StartDate", "EndDate" } ) ); }
public void LessThanShouldEvaluateSucess() { // arrange var validator = new Validator(); var instance = new StubModel() { StartDate = DateTime.Today.AddDays( -1 ), EndDate = DateTime.Today }; var context = validator.CreateContext( instance, null ); var results = new List<IValidationResult>(); validator.For<StubModel>().Property( m => m.StartDate ).LessThan( m => m.EndDate ); // act var valid = validator.TryValidateObject( instance, context, results ); // assert Assert.True( valid ); Assert.Equal( 0, results.Count ); }