本文整理汇总了C#中TestModelMetadataProvider.GetMetadataForType方法的典型用法代码示例。如果您正苦于以下问题:C# TestModelMetadataProvider.GetMetadataForType方法的具体用法?C# TestModelMetadataProvider.GetMetadataForType怎么用?C# TestModelMetadataProvider.GetMetadataForType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TestModelMetadataProvider
的用法示例。
在下文中一共展示了TestModelMetadataProvider.GetMetadataForType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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;
}
示例2: 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();
}
示例3: GetTypeNames_ReturnsExpectedResults
public void GetTypeNames_ReturnsExpectedResults(Type fieldType, string[] expectedResult)
{
// Arrange
var metadataProvider = new TestModelMetadataProvider();
var metadata = metadataProvider.GetMetadataForType(fieldType);
// Act
var typeNames = TemplateRenderer.GetTypeNames(metadata, fieldType);
// Assert
var collectionAssertions = expectedResult.Select<string, Action<string>>(expected =>
actual => Assert.Equal(expected, actual));
Assert.Collection(typeNames, collectionAssertions.ToArray());
}
示例4: CanCreateModel_ReturnsTrue_IfIsTopLevelObjectAndNotIsFirstChanceBinding
public async Task CanCreateModel_ReturnsTrue_IfIsTopLevelObjectAndNotIsFirstChanceBinding(
bool isTopLevelObject,
bool isFirstChanceBinding,
string binderModelName,
string modelName,
bool expectedCanCreate)
{
var mockValueProvider = new Mock<IValueProvider>();
mockValueProvider
.Setup(o => o.ContainsPrefixAsync(It.IsAny<string>()))
.Returns(Task.FromResult(false));
var metadataProvider = new TestModelMetadataProvider();
var bindingContext = new MutableObjectBinderContext
{
ModelBindingContext = new ModelBindingContext
{
IsTopLevelObject = isTopLevelObject,
IsFirstChanceBinding = isFirstChanceBinding,
// Random type.
ModelMetadata = metadataProvider.GetMetadataForType(typeof(Person)),
ValueProvider = mockValueProvider.Object,
OperationBindingContext = new OperationBindingContext
{
ValueProvider = mockValueProvider.Object,
MetadataProvider = metadataProvider,
ValidatorProvider = Mock.Of<IModelValidatorProvider>(),
},
// CanCreateModel() ignores the BinderModelName and ModelName properties.
BinderModelName = binderModelName,
ModelName = modelName,
},
};
var mutableBinder = new TestableMutableObjectModelBinder();
bindingContext.PropertyMetadata =
mutableBinder.GetMetadataForProperties(bindingContext.ModelBindingContext).ToArray();
// Act
var canCreate = await mutableBinder.CanCreateModel(bindingContext);
// Assert
Assert.Equal(expectedCanCreate, canCreate);
}
示例5: 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);
}
示例6: 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);
}
示例7: CreateBindingContext_FiltersValueProviders_ForValueProviderSource
public void CreateBindingContext_FiltersValueProviders_ForValueProviderSource()
{
// Arrange
var metadataProvider = new TestModelMetadataProvider();
var original = CreateDefaultValueProvider();
// Act
var context = DefaultModelBindingContext.CreateBindingContext(
new ActionContext(),
original,
metadataProvider.GetMetadataForType(typeof(object)),
new BindingInfo() { BindingSource = BindingSource.Query },
"model");
// Assert
Assert.Collection(
Assert.IsType<CompositeValueProvider>(context.ValueProvider),
vp => Assert.Same(original[1], vp));
}
示例8: 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;
}
示例9: CanCreateModel_ReturnsTrue_IfIsTopLevelObject
public void CanCreateModel_ReturnsTrue_IfIsTopLevelObject(
bool isTopLevelObject,
bool expectedCanCreate)
{
var mockValueProvider = new Mock<IValueProvider>();
mockValueProvider
.Setup(o => o.ContainsPrefix(It.IsAny<string>()))
.Returns(false);
var metadataProvider = new TestModelMetadataProvider();
var bindingContext = new MutableObjectBinderContext
{
ModelBindingContext = new ModelBindingContext
{
IsTopLevelObject = isTopLevelObject,
// Random type.
ModelMetadata = metadataProvider.GetMetadataForType(typeof(Person)),
ValueProvider = mockValueProvider.Object,
OperationBindingContext = new OperationBindingContext
{
ValueProvider = mockValueProvider.Object,
MetadataProvider = metadataProvider,
ValidatorProvider = Mock.Of<IModelValidatorProvider>(),
},
ModelState = new ModelStateDictionary(),
},
};
var mutableBinder = new TestableMutableObjectModelBinder();
bindingContext.PropertyMetadata =
mutableBinder.GetMetadataForProperties(bindingContext.ModelBindingContext).ToArray();
// Act
var canCreate = mutableBinder.CanCreateModel(bindingContext);
// Assert
Assert.Equal(expectedCanCreate, canCreate);
}
示例10: CanCreateModel_CreatesModel_ForTopLevelObjectIfThereIsExplicitPrefix
public async Task CanCreateModel_CreatesModel_ForTopLevelObjectIfThereIsExplicitPrefix(
Type modelType,
bool isPrefixProvided)
{
var mockValueProvider = new Mock<IValueProvider>();
mockValueProvider.Setup(o => o.ContainsPrefixAsync(It.IsAny<string>()))
.Returns(Task.FromResult(false));
var metadataProvider = new TestModelMetadataProvider();
var bindingContext = new MutableObjectBinderContext
{
ModelBindingContext = new ModelBindingContext
{
// Random type.
ModelMetadata = metadataProvider.GetMetadataForType(typeof(Person)),
ValueProvider = mockValueProvider.Object,
OperationBindingContext = new OperationBindingContext
{
ValueProvider = mockValueProvider.Object,
MetadataProvider = metadataProvider,
ValidatorProvider = Mock.Of<IModelValidatorProvider>(),
},
// Setting it to empty ensures that model does not get created becasue of no model name.
ModelName = "dummyModelName",
BinderModelName = isPrefixProvided ? "prefix" : null,
}
};
var mutableBinder = new TestableMutableObjectModelBinder();
bindingContext.PropertyMetadata = mutableBinder.GetMetadataForProperties(
bindingContext.ModelBindingContext);
// Act
var retModel = await mutableBinder.CanCreateModel(bindingContext);
// Assert
Assert.Equal(isPrefixProvided, retModel);
}
示例11: 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);
}
示例12: ArrayModelBinder_CreatesEmptyCollection_IfIsTopLevelObject
public async Task ArrayModelBinder_CreatesEmptyCollection_IfIsTopLevelObject()
{
// Arrange
var binder = new ArrayModelBinder<string>(new SimpleTypeModelBinder(typeof(string)));
var bindingContext = CreateContext();
bindingContext.IsTopLevelObject = true;
// Lack of prefix and non-empty model name both ignored.
bindingContext.ModelName = "modelName";
var metadataProvider = new TestModelMetadataProvider();
bindingContext.ModelMetadata = metadataProvider.GetMetadataForType(typeof(string[]));
bindingContext.ValueProvider = new TestValueProvider(new Dictionary<string, object>());
// Act
await binder.BindModelAsync(bindingContext);
// Assert
Assert.Empty(Assert.IsType<string[]>(bindingContext.Result.Model));
Assert.True(bindingContext.Result.IsModelSet);
}
示例13: GetChildModelBindingContext
public void GetChildModelBindingContext()
{
// Arrange
var originalBindingContext = new ModelBindingContext
{
ModelMetadata = new TestModelMetadataProvider().GetMetadataForType(typeof(object)),
ModelName = "theName",
ModelState = new ModelStateDictionary(),
ValueProvider = new SimpleHttpValueProvider()
};
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.GetChildModelBindingContext(
originalBindingContext,
string.Empty,
newModelMetadata);
// Assert
Assert.Same(newModelMetadata, newBindingContext.ModelMetadata);
Assert.Same(newModelMetadata.BindingSource, newBindingContext.BindingSource);
Assert.Same(newModelMetadata.BinderModelName, newBindingContext.BinderModelName);
Assert.Same(newModelMetadata.BinderType, newBindingContext.BinderType);
Assert.Equal("", newBindingContext.ModelName);
Assert.Equal(originalBindingContext.ModelState, newBindingContext.ModelState);
Assert.Equal(originalBindingContext.ValueProvider, newBindingContext.ValueProvider);
}
示例14: ProcessAsyncInTemplate_WithItems_GeneratesExpectedOutput_DoesNotChangeSelectList
public async Task ProcessAsyncInTemplate_WithItems_GeneratesExpectedOutput_DoesNotChangeSelectList(
object model,
Type containerType,
Func<object> modelAccessor,
NameAndId nameAndId,
string expectedOptions)
{
// Arrange
var originalAttributes = new Dictionary<string, object>
{
{ "class", "form-control" },
};
var originalPostContent = "original content";
var expectedAttributes = new Dictionary<string, object>(originalAttributes)
{
{ "id", nameAndId.Id },
{ "name", nameAndId.Name },
{ "valid", "from validation attributes" },
};
var expectedPreContent = "original pre-content";
var expectedContent = "original content";
var expectedPostContent = originalPostContent + expectedOptions;
var expectedTagName = "select";
var metadataProvider = new TestModelMetadataProvider();
var containerMetadata = metadataProvider.GetMetadataForType(containerType);
var containerExplorer = metadataProvider.GetModelExplorerForType(containerType, model);
var propertyMetadata = metadataProvider.GetMetadataForProperty(containerType, "Text");
var modelExplorer = containerExplorer.GetExplorerForExpression(propertyMetadata, modelAccessor());
var modelExpression = new ModelExpression(name: string.Empty, modelExplorer: modelExplorer);
var tagHelperContext = new TagHelperContext(
allAttributes: new Dictionary<string, object>(),
items: new Dictionary<object, object>(),
uniqueId: "test",
getChildContentAsync: () =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
var output = new TagHelperOutput(expectedTagName, originalAttributes)
{
SelfClosing = true
};
output.PreContent.SetContent(expectedPreContent);
output.Content.SetContent(expectedContent);
output.PostContent.SetContent(originalPostContent);
var htmlGenerator = new TestableHtmlGenerator(metadataProvider)
{
ValidationAttributes =
{
{ "valid", "from validation attributes" },
}
};
var viewContext = TestableHtmlGenerator.GetViewContext(model, htmlGenerator, metadataProvider);
viewContext.ViewData.TemplateInfo.HtmlFieldPrefix = nameAndId.Name;
var items = new SelectList(new[] { "", "outer text", "inner text", "other text" });
var savedDisabled = items.Select(item => item.Disabled).ToList();
var savedGroup = items.Select(item => item.Group).ToList();
var savedSelected = items.Select(item => item.Selected).ToList();
var savedText = items.Select(item => item.Text).ToList();
var savedValue = items.Select(item => item.Value).ToList();
var tagHelper = new SelectTagHelper
{
For = modelExpression,
Generator = htmlGenerator,
Items = items,
ViewContext = viewContext,
};
// Act
await tagHelper.ProcessAsync(tagHelperContext, output);
// Assert
Assert.True(output.SelfClosing);
Assert.Equal(expectedAttributes, output.Attributes);
Assert.Equal(expectedPreContent, output.PreContent.GetContent());
Assert.Equal(expectedContent, output.Content.GetContent());
Assert.Equal(expectedPostContent, output.PostContent.GetContent());
Assert.Equal(expectedTagName, output.TagName);
Assert.NotNull(viewContext.FormContext?.FormData);
Assert.Single(
viewContext.FormContext.FormData,
entry => entry.Key == SelectTagHelper.SelectedValuesFormDataKey);
Assert.Equal(savedDisabled, items.Select(item => item.Disabled));
Assert.Equal(savedGroup, items.Select(item => item.Group));
Assert.Equal(savedSelected, items.Select(item => item.Selected));
Assert.Equal(savedText, items.Select(item => item.Text));
Assert.Equal(savedValue, items.Select(item => item.Value));
}
示例15: CreateBinder_CreatesNoOpBinder_WhenPropertyDoesntHaveABinder
public void CreateBinder_CreatesNoOpBinder_WhenPropertyDoesntHaveABinder()
{
// Arrange
var metadataProvider = new TestModelMetadataProvider();
// There isn't a provider that can handle WidgetId.
var options = new TestOptionsManager<MvcOptions>();
options.Value.ModelBinderProviders.Add(new TestModelBinderProvider(c =>
{
if (c.Metadata.ModelType == typeof(Widget))
{
Assert.NotNull(c.CreateBinder(c.Metadata.Properties[nameof(Widget.Id)]));
return Mock.Of<IModelBinder>();
}
return null;
}));
var factory = new ModelBinderFactory(metadataProvider, options);
var context = new ModelBinderFactoryContext()
{
Metadata = metadataProvider.GetMetadataForType(typeof(Widget)),
};
// Act
var result = factory.CreateBinder(context);
// Assert
Assert.NotNull(result);
}