本文整理汇总了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);
}
示例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);
}
示例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);
}
示例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;
}
示例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();
}
示例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);
}
示例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));
}
示例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);
}
示例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);
}
示例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);
}
示例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;
}
示例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"));
}
示例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);
}
示例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);
}
示例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);
}