我有一个自定义 TagHelper,它扩展了 OOTB InputTagHelper。我根据与其关联的模型属性上存在的自定义 ValidationAttribute 有条件地向其添加属性。TagHelper.Process方法中的代码工作正常:
if (this.For.Metadata.ValidatorMetadata.OfType<NumericValidationAttribute>().Any())
{
output?.Attributes.SetAttribute(new TagHelperAttribute("inputmode", "numeric"));
output?.Attributes.SetAttribute(new TagHelperAttribute("pattern", "[0-9]*"));
}
Run Code Online (Sandbox Code Playgroud)
我的问题是在单元测试中。我使用 Net Core MVC 测试存储库中提供的代码编写了其他单元测试:https : //github.com/aspnet/Mvc/blob/master/test/Microsoft.AspNetCore.Mvc.TagHelpers.Test/
...但没有真正指导如何为我要测试的属性创建Forie ModelExpression,该属性具有与之关联的 Validation 属性:例如
public class TestModel
{
[NumericValidation(ErrorMessage = "Error message")]
public string Field1 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我希望能够填充For.Metadata.ValidatorMetadata此 ModelExpression的列表,但我不知道如何。
不起作用的完整单元测试是:
[Fact]
public void CustomInputTagHelperProcess_NumericValidationAttributeOnModelProperty_GeneratesCorrectHtml()
{
// Arrange
var metadataProvider = new EmptyModelMetadataProvider();
var htmlGenerator = new TestableHtmlGenerator(metadataProvider);
var model = new TestModel
{
Field1 = "cc", …Run Code Online (Sandbox Code Playgroud) unit-testing validationattribute asp.net-core-mvc asp.net-core-tag-helpers