当前位置: 首页>>代码示例>>C#>>正文


C# Validator.CreateContext方法代码示例

本文整理汇总了C#中Validator.CreateContext方法的典型用法代码示例。如果您正苦于以下问题:C# Validator.CreateContext方法的具体用法?C# Validator.CreateContext怎么用?C# Validator.CreateContext使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Validator的用法示例。


在下文中一共展示了Validator.CreateContext方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CreateContextShouldNotAllowNullInstance

        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 );
        }
开发者ID:WaffleSquirrel,项目名称:More,代码行数:12,代码来源:ValidatorTest.cs

示例2: TryValidatePropertyShouldReturnTrueForTypeWithoutRules

        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 );
        }
开发者ID:WaffleSquirrel,项目名称:More,代码行数:16,代码来源:ValidatorTest.cs

示例3: ValidatePropertyShouldThrowExceptionForInvalidValue

        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 );
        }
开发者ID:WaffleSquirrel,项目名称:More,代码行数:19,代码来源:ValidatorTest.cs

示例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 );
        }
开发者ID:WaffleSquirrel,项目名称:More,代码行数:17,代码来源:ValidatorTest.cs

示例5: TryValidatePropertyShouldReturnExpectedResult

        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 );
        }
开发者ID:WaffleSquirrel,项目名称:More,代码行数:18,代码来源:ValidatorTest.cs

示例6: 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" } ) );
        }
开发者ID:WaffleSquirrel,项目名称:More,代码行数:20,代码来源:IValidationBuilderExtensionsTest.cs

示例7: 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 );
        }
开发者ID:WaffleSquirrel,项目名称:More,代码行数:17,代码来源:IValidationBuilderExtensionsTest.cs

示例8: 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" } ) );
        }
开发者ID:WaffleSquirrel,项目名称:More,代码行数:19,代码来源:IValidationBuilderExtensionsTest.cs

示例9: 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" } ) );
        }
开发者ID:WaffleSquirrel,项目名称:More,代码行数:19,代码来源:IValidationBuilderExtensionsTest.cs

示例10: 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 );
        }
开发者ID:WaffleSquirrel,项目名称:More,代码行数:17,代码来源:IValidationBuilderExtensionsTest.cs


注:本文中的Validator.CreateContext方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。