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


C# TestModelMetadataProvider.ForType方法代码示例

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


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

示例1: BindModel_NoInputFormatterFound_SetsModelStateError

        public async Task BindModel_NoInputFormatterFound_SetsModelStateError()
        {
            // Arrange
            var provider = new TestModelMetadataProvider();
            provider.ForType<Person>().BindingDetails(d => d.BindingSource = BindingSource.Body);

            var bindingContext = GetBindingContext(typeof(Person), metadataProvider: provider);

            var binder = bindingContext.OperationBindingContext.ModelBinder;

            // Act
            var binderResult = await binder.BindModelAsync(bindingContext);

            // Assert

            // Returns non-null because it understands the metadata type.
            Assert.NotNull(binderResult);
            Assert.False(binderResult.IsModelSet);
            Assert.Null(binderResult.Model);

            // Key is empty because this was a top-level binding.
            var entry = Assert.Single(bindingContext.ModelState);
            Assert.Equal(string.Empty, entry.Key);
            Assert.Single(entry.Value.Errors);
        }
开发者ID:huoxudong125,项目名称:Mvc,代码行数:25,代码来源:BodyModelBinderTests.cs

示例2: BindModel_NoInputFormatterFound_SetsModelStateError_RespectsBinderModelName

        public async Task BindModel_NoInputFormatterFound_SetsModelStateError_RespectsBinderModelName()
        {
            // Arrange
            var provider = new TestModelMetadataProvider();
            provider.ForType<Person>().BindingDetails(d => d.BindingSource = BindingSource.Body);

            var bindingContext = GetBindingContext(typeof(Person), metadataProvider: provider);
            bindingContext.BinderModelName = "custom";

            var binder = CreateBinder();

            // Act
            var binderResult = await binder.BindModelResultAsync(bindingContext);

            // Assert

            // Returns non-null because it understands the metadata type.
            Assert.NotNull(binderResult);
            Assert.False(binderResult.IsModelSet);
            Assert.Null(binderResult.Model);

            // Key is the bindermodelname because this was a top-level binding.
            var entry = Assert.Single(bindingContext.ModelState);
            Assert.Equal("custom", entry.Key);
            Assert.Single(entry.Value.Errors);
        }
开发者ID:cemalshukriev,项目名称:Mvc,代码行数:26,代码来源:BodyModelBinderTests.cs

示例3: BindModel_CallsSelectedInputFormatterOnce

        public async Task BindModel_CallsSelectedInputFormatterOnce()
        {
            // Arrange
            var mockInputFormatter = new Mock<IInputFormatter>();
            mockInputFormatter.Setup(f => f.CanRead(It.IsAny<InputFormatterContext>()))
                .Returns(true)
                .Verifiable();
            mockInputFormatter.Setup(o => o.ReadAsync(It.IsAny<InputFormatterContext>()))
                              .Returns(InputFormatterResult.SuccessAsync(new Person()))
                              .Verifiable();
            var inputFormatter = mockInputFormatter.Object;

            var provider = new TestModelMetadataProvider();
            provider.ForType<Person>().BindingDetails(d => d.BindingSource = BindingSource.Body);

            var bindingContext = GetBindingContext(
                typeof(Person),
                new[] { inputFormatter },
                metadataProvider: provider);

            var binder = new BodyModelBinder();

            // Act
            var binderResult = await binder.BindModelAsync(bindingContext);

            // Assert
            mockInputFormatter.Verify(v => v.CanRead(It.IsAny<InputFormatterContext>()), Times.Once);
            mockInputFormatter.Verify(v => v.ReadAsync(It.IsAny<InputFormatterContext>()), Times.Once);
            Assert.NotNull(binderResult);
            Assert.True(binderResult.IsModelSet);
        }
开发者ID:huoxudong125,项目名称:Mvc,代码行数:31,代码来源:BodyModelBinderTests.cs

示例4: GetBindingContext

        private static DefaultModelBindingContext GetBindingContext(Type modelType)
        {
            var metadataProvider = new TestModelMetadataProvider();
            metadataProvider.ForType(modelType).BindingDetails(d => d.BindingSource = BindingSource.Services);
            var modelMetadata = metadataProvider.GetMetadataForType(modelType);

            var services = new ServiceCollection();
            services.AddSingleton<IService>(new Service());

            var bindingContext = new DefaultModelBindingContext
            {
                ActionContext = new ActionContext()
                {
                    HttpContext = new DefaultHttpContext()
                    {
                        RequestServices = services.BuildServiceProvider(),
                    }
                },
                ModelMetadata = modelMetadata,
                ModelName = "modelName",
                FieldName = "modelName",
                ModelState = new ModelStateDictionary(),
                BinderModelName = modelMetadata.BinderModelName,
                BindingSource = modelMetadata.BindingSource,
                ValidationState = new ValidationStateDictionary(),
            };

            return bindingContext;
        }
开发者ID:xuchrist,项目名称:Mvc,代码行数:29,代码来源:ServicesModelBinderTest.cs

示例5: EnterNestedScope_CopiesProperties

        public void EnterNestedScope_CopiesProperties()
        {
            // Arrange
            var bindingContext = new DefaultModelBindingContext
            {
                Model = new object(),
                ModelMetadata = new TestModelMetadataProvider().GetMetadataForType(typeof(object)),
                ModelName = "theName",
                OperationBindingContext = new OperationBindingContext(),
                ValueProvider = new SimpleValueProvider(),
                ModelState = new ModelStateDictionary(),
            };

            var metadataProvider = new TestModelMetadataProvider();
            metadataProvider.ForType<object>().BindingDetails(d =>
                {
                    d.BindingSource = BindingSource.Custom;
                    d.BinderType = typeof(TestModelBinder);
                    d.BinderModelName = "custom";
                });

            var newModelMetadata = metadataProvider.GetMetadataForType(typeof(object));

            // Act
            var originalBinderModelName = bindingContext.BinderModelName;
            var originalBindingSource = bindingContext.BindingSource;
            var originalModelState = bindingContext.ModelState;
            var originalOperationBindingContext = bindingContext.OperationBindingContext;
            var originalValueProvider = bindingContext.ValueProvider;

            var disposable = bindingContext.EnterNestedScope(
                modelMetadata: newModelMetadata,
                fieldName: "fieldName",
                modelName: "modelprefix.fieldName",
                model: null);

            // Assert
            Assert.Same(newModelMetadata.BinderModelName, bindingContext.BinderModelName);
            Assert.Same(newModelMetadata.BindingSource, bindingContext.BindingSource);
            Assert.Equal("fieldName", bindingContext.FieldName);
            Assert.False(bindingContext.IsTopLevelObject);
            Assert.Null(bindingContext.Model);
            Assert.Same(newModelMetadata, bindingContext.ModelMetadata);
            Assert.Equal("modelprefix.fieldName", bindingContext.ModelName);
            Assert.Same(originalModelState, bindingContext.ModelState);
            Assert.Same(originalOperationBindingContext, bindingContext.OperationBindingContext);
            Assert.Same(originalValueProvider, bindingContext.ValueProvider);

            disposable.Dispose();
        }
开发者ID:cemalshukriev,项目名称:Mvc,代码行数:50,代码来源:DefaultModelBindingContextTest.cs

示例6: DisplayTextFor_ReturnsNullDisplayText_IfSetAndValueNull

        public void DisplayTextFor_ReturnsNullDisplayText_IfSetAndValueNull()
        {
            // Arrange
            var provider = new TestModelMetadataProvider();
            provider.ForType<OverriddenToStringModel>().DisplayDetails(dd =>
            {
                dd.NullDisplayText = "Null display Text";
            });

            var helper = DefaultTemplatesUtilities.GetHtmlHelper<OverriddenToStringModel>(model: null, provider: provider);

            // Act
            var result = helper.DisplayTextFor(m => m);

            // Assert
            Assert.Equal("Null display Text", result);
        }
开发者ID:4myBenefits,项目名称:Mvc,代码行数:17,代码来源:HtmlHelperDisplayTextTest.cs

示例7: ObjectTemplateDisplaysNullDisplayTextWhenObjectIsNull

        public void ObjectTemplateDisplaysNullDisplayTextWhenObjectIsNull()
        {
            // Arrange
            var provider = new TestModelMetadataProvider();
            provider.ForType<DefaultTemplatesUtilities.ObjectTemplateModel>().DisplayDetails(dd =>
            {
                dd.NullDisplayText = "(null value)";
            });

            var html = DefaultTemplatesUtilities.GetHtmlHelper(provider: provider);

            // Act
            var result = DefaultDisplayTemplates.ObjectTemplate(html);

            // Assert
            Assert.Equal("(null value)", HtmlContentUtilities.HtmlContentToString(result));
        }
开发者ID:4myBenefits,项目名称:Mvc,代码行数:17,代码来源:DefaultDisplayTemplatesTest.cs

示例8: BindModel_IsGreedy

        public async Task BindModel_IsGreedy()
        {
            // Arrange
            var provider = new TestModelMetadataProvider();
            provider.ForType<Person>().BindingDetails(d => d.BindingSource = BindingSource.Body);

            var bindingContext = GetBindingContext(typeof(Person), metadataProvider: provider);

            var binder = bindingContext.OperationBindingContext.ModelBinder;

            // Act
            var binderResult = await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.NotNull(binderResult);
            Assert.False(binderResult.IsModelSet);
        }
开发者ID:AndersBillLinden,项目名称:Mvc,代码行数:17,代码来源:BodyModelBinderTests.cs

示例9: CreateChildBindingContext_CopiesProperties

        public void CreateChildBindingContext_CopiesProperties()
        {
            // Arrange
            var originalBindingContext = new ModelBindingContext
            {
                Model = new object(),
                ModelMetadata = new TestModelMetadataProvider().GetMetadataForType(typeof(object)),
                ModelName = "theName",
                OperationBindingContext = new OperationBindingContext(),
                ValueProvider = new SimpleValueProvider(),
                ModelState = new ModelStateDictionary(),
            };

            var metadataProvider = new TestModelMetadataProvider();
            metadataProvider.ForType<object>().BindingDetails(d =>
                {
                    d.BindingSource = BindingSource.Custom;
                    d.BinderType = typeof(TestModelBinder);
                    d.BinderModelName = "custom";
                });

            var newModelMetadata = metadataProvider.GetMetadataForType(typeof(object));
            
            // Act
            var newBindingContext = ModelBindingContext.CreateChildBindingContext(
                originalBindingContext,
                newModelMetadata,
                fieldName: "fieldName",
                modelName: "modelprefix.fieldName",
                model: null);

            // Assert
            Assert.Same(newModelMetadata.BinderModelName, newBindingContext.BinderModelName);
            Assert.Same(newModelMetadata.BinderType, newBindingContext.BinderType);
            Assert.Same(newModelMetadata.BindingSource, newBindingContext.BindingSource);
            Assert.False(newBindingContext.FallbackToEmptyPrefix);
            Assert.Equal("fieldName", newBindingContext.FieldName);
            Assert.False(newBindingContext.IsTopLevelObject);
            Assert.Null(newBindingContext.Model);
            Assert.Same(newModelMetadata, newBindingContext.ModelMetadata);
            Assert.Equal("modelprefix.fieldName", newBindingContext.ModelName);
            Assert.Same(originalBindingContext.ModelState, newBindingContext.ModelState);
            Assert.Same(originalBindingContext.OperationBindingContext, newBindingContext.OperationBindingContext);
            Assert.Same(originalBindingContext.ValueProvider, newBindingContext.ValueProvider);
        }
开发者ID:huoxudong125,项目名称:Mvc,代码行数:45,代码来源:ModelBindingContextTest.cs

示例10: BindingSourceModelBinder_ReturnsFalse_NonMatchingSource

        public async Task BindingSourceModelBinder_ReturnsFalse_NonMatchingSource()
        {
            // Arrange
            var provider = new TestModelMetadataProvider();
            provider.ForType<string>().BindingDetails(d => d.BindingSource = BindingSource.Query);

            var context = new ModelBindingContext();
            context.ModelMetadata = provider.GetMetadataForType(typeof(string));

            var binder = new TestableBindingSourceModelBinder(BindingSource.Body);

            // Act 
            var result = await binder.BindModelAsync(context);

            // Assert
            Assert.Null(result);
            Assert.False(binder.WasBindModelCoreCalled);
        }
开发者ID:RehanSaeed,项目名称:Mvc,代码行数:18,代码来源:BindingSourceModelBinderTest.cs

示例11: GetBindingContext

        private static ModelBindingContext GetBindingContext(Type modelType)
        {
            var metadataProvider = new TestModelMetadataProvider();
            metadataProvider.ForType(modelType).BindingDetails(d => d.BindingSource = BindingSource.Header);
            var modelMetadata = metadataProvider.GetMetadataForType(modelType);
            var bindingContext = new ModelBindingContext
            {
                ModelMetadata = modelMetadata,
                ModelName = "modelName",
                OperationBindingContext = new OperationBindingContext
                {
                    ModelBinder = new HeaderModelBinder(),
                    MetadataProvider = metadataProvider,
                    HttpContext = new DefaultHttpContext()
                },
                BinderModelName = modelMetadata.BinderModelName,
                BindingSource = modelMetadata.BindingSource,
            };

            return bindingContext;
        }
开发者ID:njannink,项目名称:sonarlint-vs,代码行数:21,代码来源:HeaderModelBinderTests.cs

示例12: BindModel_NoInputFormatterFound_SetsModelStateError

        public async Task BindModel_NoInputFormatterFound_SetsModelStateError()
        {
            // Arrange
            var provider = new TestModelMetadataProvider();
            provider.ForType<Person>().BindingDetails(d => d.BindingSource = BindingSource.Body);

            var bindingContext = GetBindingContext(typeof(Person), metadataProvider: provider);

            var binder = bindingContext.OperationBindingContext.ModelBinder;

            // Act
            var binderResult = await binder.BindModelAsync(bindingContext);

            // Assert

            // Returns true because it understands the metadata type.
            Assert.NotNull(binderResult);
            Assert.False(binderResult.IsModelSet);
            Assert.Null(binderResult.Model);
            Assert.True(bindingContext.ModelState.ContainsKey("someName"));
        }
开发者ID:AndersBillLinden,项目名称:Mvc,代码行数:21,代码来源:BodyModelBinderTests.cs

示例13: BindModel_NoInputFormatterFound_SetsModelStateError

        public async Task BindModel_NoInputFormatterFound_SetsModelStateError()
        {
            // Arrange
            var provider = new TestModelMetadataProvider();
            provider.ForType<Person>().BindingDetails(d => d.BindingSource = BindingSource.Body);

            var bindingContext = GetBindingContext(typeof(Person), metadataProvider: provider);

            var binder = CreateBinder(new List<IInputFormatter>());

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.False(bindingContext.Result.IsModelSet);
            Assert.Null(bindingContext.Result.Model);

            // Key is the empty string because this was a top-level binding.
            var entry = Assert.Single(bindingContext.ModelState);
            Assert.Equal(string.Empty, entry.Key);
            Assert.Single(entry.Value.Errors);
        }
开发者ID:xuchrist,项目名称:Mvc,代码行数:22,代码来源:BodyModelBinderTests.cs

示例14: BindModel_CallsSelectedInputFormatterOnce

        public async Task BindModel_CallsSelectedInputFormatterOnce()
        {
            // Arrange
            var mockInputFormatter = new Mock<IInputFormatter>();
            mockInputFormatter.Setup(f => f.CanRead(It.IsAny<InputFormatterContext>()))
                .Returns(true)
                .Verifiable();
            mockInputFormatter.Setup(o => o.ReadAsync(It.IsAny<InputFormatterContext>()))
                              .Returns(Task.FromResult<object>(new Person()))
                              .Verifiable();
            var inputFormatter = mockInputFormatter.Object;

            var provider = new TestModelMetadataProvider();
            provider.ForType<Person>().BindingDetails(d => d.BindingSource = BindingSource.Body);

            var bindingContext = GetBindingContext(
                typeof(Person),
                new[] { inputFormatter },
                metadataProvider: provider);

            var binder = new BodyModelBinder();

            // Act
            var binderResult = await binder.BindModelAsync(bindingContext);

            // Assert
            mockInputFormatter.Verify(v => v.CanRead(It.IsAny<InputFormatterContext>()), Times.Once);
            mockInputFormatter.Verify(v => v.ReadAsync(It.IsAny<InputFormatterContext>()), Times.Once);
            Assert.NotNull(binderResult);
            Assert.True(binderResult.IsModelSet);
            Assert.NotNull(binderResult.ValidationNode);
            Assert.True(binderResult.ValidationNode.ValidateAllProperties);
            Assert.False(binderResult.ValidationNode.SuppressValidation);
            Assert.Empty(binderResult.ValidationNode.ChildNodes);
            Assert.Equal(binderResult.Key, binderResult.ValidationNode.Key);
            Assert.Equal(bindingContext.ModelMetadata, binderResult.ValidationNode.ModelMetadata);
            Assert.Same(binderResult.Model, binderResult.ValidationNode.Model);
        }
开发者ID:RehanSaeed,项目名称:Mvc,代码行数:38,代码来源:BodyModelBinderTests.cs

示例15: BindingSourceModelBinder_ReturnsTrue_MatchingSource

        public async Task BindingSourceModelBinder_ReturnsTrue_MatchingSource()
        {
            // Arrange
            var provider = new TestModelMetadataProvider();
            provider.ForType<string>().BindingDetails(d => d.BindingSource = BindingSource.Body);
            var modelMetadata = provider.GetMetadataForType(typeof(string));
            var context = new ModelBindingContext()
            {
                ModelMetadata = modelMetadata,
                BindingSource = modelMetadata.BindingSource,
                BinderModelName = modelMetadata.BinderModelName
            };

            var binder = new TestableBindingSourceModelBinder(BindingSource.Body);

            // Act 
            var result = await binder.BindModelAsync(context);

            // Assert
            Assert.NotNull(result);
            Assert.True(result.IsModelSet);
            Assert.True(binder.WasBindModelCoreCalled);
        }
开发者ID:RehanSaeed,项目名称:Mvc,代码行数:23,代码来源:BindingSourceModelBinderTest.cs


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