本文整理汇总了C#中Validator.TryValidateObject方法的典型用法代码示例。如果您正苦于以下问题:C# Validator.TryValidateObject方法的具体用法?C# Validator.TryValidateObject怎么用?C# Validator.TryValidateObject使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Validator
的用法示例。
在下文中一共展示了Validator.TryValidateObject方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TryValidateObjectShouldReturnTrueForInstanceWithoutRules
public void TryValidateObjectShouldReturnTrueForInstanceWithoutRules()
{
// arrange
var instance = new Mock<IComponent>().Object;
var validator = new Validator();
var context = validator.CreateContext( instance, null );
var results = new List<IValidationResult>();
// act
var valid = validator.TryValidateObject( instance, context, results );
// assert
Assert.True( valid );
Assert.Equal( 0, results.Count );
}
示例2: TryValidationObjectShouldNotAllowNullContext
public void TryValidationObjectShouldNotAllowNullContext()
{
// arrange
IValidationContext context = null;
var validator = new Validator();
// act
var ex = Assert.Throws<ArgumentNullException>( () => validator.TryValidateObject( null, context, null ) );
// assert
Assert.Equal( "validationContext", ex.ParamName );
}
示例3: TryValidateObjectShouldReturnExpectedResult
public void TryValidateObjectShouldReturnExpectedResult( IComponent instance, int resultCount, bool expected )
{
// arrange
var validator = new Validator();
var context = validator.CreateContext( instance, null );
var results = new List<IValidationResult>();
validator.For<IComponent>().Property( c => c.Site ).Required();
// act
var actual = validator.TryValidateObject( instance, context, results );
// assert
Assert.Equal( expected, actual );
Assert.Equal( resultCount, results.Count );
}
示例4: TryValidateObjectShouldReturnTrueForNullInstance
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 );
}
示例5: GreaterThanOrEqualToShouldEvaluateWithCustomErrorMessage
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" } ) );
}
示例6: GreaterThanOrEqualToShouldEvaluateSuccess
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 );
}
示例7: GreaterThanShouldEvaluateExpectedResult
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" } ) );
}
示例8: LessThanOrEqualToShouldEvaluateExpectedResult
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" } ) );
}
示例9: LessThanShouldEvaluateSucess
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 );
}