gpt4 book ai didi

c# - 模拟 ViewContext 以测试验证错误消息

转载 作者:行者123 更新时间:2023-11-30 20:56:52 27 4
gpt4 key购买 nike

这是故事。为了能够将格式良好的 Bootstrap 控件放入我的 MVC 表单中,我正在构建一个 HtmlHelper使用单个命令生成以下结构的扩展方法:

<div class="control-group">
@Html.LabelFor(m => m.UserName, new { @class = "control-label" })
<div class="controls">
<div class="input-prepend">
<span class="add-on"><i class="icon-user"></i></span>
@Html.TextBoxFor(m => m.UserName, new { @class = "input-xlarge" })
</div>
@Html.ValidationMessageFor(m => m.UserName)
</div>
</div>

方法本身并不难写。更困难的是单元测试。为了使我的扩展方法可测试,我需要创建 HtmlHelper<T> 的实例使用适当的模拟。为此,我调整了一个 StackOverflow 旧问题的答案并提出了这个问题:

public static HtmlHelper<TModel> CreateHtmlHelper<TModel>(bool clientValidationEnabled, bool unobtrusiveJavascriptEnabled, ViewDataDictionary dictionary = null)
{
if (dictionary == null)
dictionary = new ViewDataDictionary { TemplateInfo = new TemplateInfo() };

var mockViewContext = new Mock<ViewContext>(
new ControllerContext(
new Mock<HttpContextBase>().Object,
new RouteData(),
new Mock<ControllerBase>().Object),
new Mock<IView>().Object,
dictionary,
new TempDataDictionary(),
new Mock<TextWriter>().Object);

mockViewContext.SetupGet(c => c.UnobtrusiveJavaScriptEnabled).Returns(unobtrusiveJavascriptEnabled);
mockViewContext.SetupGet(c => c.FormContext).Returns(new FormContext { FormId = "myForm" });
mockViewContext.SetupGet(c => c.ClientValidationEnabled).Returns(clientValidationEnabled);
mockViewContext.SetupGet(c => c.ViewData).Returns(dictionary);
var mockViewDataContainer = new Mock<IViewDataContainer>();
mockViewDataContainer.Setup(v => v.ViewData).Returns(dictionary);

return new HtmlHelper<TModel>(mockViewContext.Object, mockViewDataContainer.Object);
}

到目前为止一切顺利。现在我可以创建一个 HtmlHelper对象,我可以按如下方式执行我的测试:

// ARRANGE
ModelMetadataProviders.Current = new DataAnnotationsModelMetadataProvider();
var helper = MvcMocks.CreateHtmlHelper<TestModel>(true, true);
helper.ViewData.Model = new TestModel { Field = null };
helper.ViewData.ModelState.AddModelError("Field", "The field must be assigned.");

// ACT
var controlGroup = helper.ControlGroupFor(m => m.Field, CssClasses.IconUser).ToHtmlString();

问题来了。内 ControlGroupFor , 签名是

    public static HtmlString ControlGroupFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string iconClass)

我还没有完成(作为一个优秀的 TDD 开发人员),我正在调用 var validationMessage = html.ValidationMessageFor(expression) .尽管我使用了 AddModelError , ValidationMessageFor方法似乎认为 html.ViewData.ModelState["Field"]为 null 或其 ModelErrors集合为空。我推断这是因为 validationMessage 的值是

<span class="field-validation-valid" data-valmsg-for="Field" data-valmsg-replace="true"></span>

根据 Resharper,ValidationMessageFor方法向下调用此方法:

    private static MvcHtmlString ValidationMessageHelper(this HtmlHelper htmlHelper, ModelMetadata modelMetadata, string expression, string validationMessage, IDictionary<string, object> htmlAttributes)
{
string modelName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(expression);
FormContext formContext = htmlHelper.ViewContext.GetFormContextForClientValidation();

if (!htmlHelper.ViewData.ModelState.ContainsKey(modelName) && formContext == null)
{
return null;
}

ModelState modelState = htmlHelper.ViewData.ModelState[modelName];
ModelErrorCollection modelErrors = (modelState == null) ? null : modelState.Errors;
ModelError modelError = (((modelErrors == null) || (modelErrors.Count == 0)) ? null : modelErrors.FirstOrDefault(m => !String.IsNullOrEmpty(m.ErrorMessage)) ?? modelErrors[0]);

if (modelError == null && formContext == null)
{
return null;
}

TagBuilder builder = new TagBuilder("span");
builder.MergeAttributes(htmlAttributes);
builder.AddCssClass((modelError != null) ? HtmlHelper.ValidationMessageCssClassName : HtmlHelper.ValidationMessageValidCssClassName);

if (!String.IsNullOrEmpty(validationMessage))
{
builder.SetInnerText(validationMessage);
}
else if (modelError != null)
{
builder.SetInnerText(GetUserErrorMessageOrDefault(htmlHelper.ViewContext.HttpContext, modelError, modelState));
}

if (formContext != null)
{
bool replaceValidationMessageContents = String.IsNullOrEmpty(validationMessage);

if (htmlHelper.ViewContext.UnobtrusiveJavaScriptEnabled)
{
builder.MergeAttribute("data-valmsg-for", modelName);
builder.MergeAttribute("data-valmsg-replace", replaceValidationMessageContents.ToString().ToLowerInvariant());
}
else
{
FieldValidationMetadata fieldMetadata = ApplyFieldValidationMetadata(htmlHelper, modelMetadata, modelName);
// rules will already have been written to the metadata object
fieldMetadata.ReplaceValidationMessageContents = replaceValidationMessageContents; // only replace contents if no explicit message was specified

// client validation always requires an ID
builder.GenerateId(modelName + "_validationMessage");
fieldMetadata.ValidationMessageId = builder.Attributes["id"];
}
}

return builder.ToMvcHtmlString(TagRenderMode.Normal);
}

现在,根据我所做的一切,validationMessage应该给我一个spanfield-validation-error以及一条错误消息“必须分配该字段”。在我的 watch 窗口中,html.ViewData.ModelState["Field"].Errors计数为 1。我一定是遗漏了什么。谁能看出这是什么?

最佳答案

我修改了测试以直接使用 ViewContext.ViewData 而不是 ViewData:

// ARRANGE
ModelMetadataProviders.Current = new DataAnnotationsModelMetadataProvider();
var helper = MvcMocks.CreateHtmlHelper<TestModel>(true, true);
helper.ViewContext.ViewData.Model = new TestModel { Field = null };
helper.ViewContext.ViewData.ModelState.AddModelError("Field", "The field must be assigned.");

// ACT
var controlGroup = helper.ControlGroupFor(m => m.Field, CssClasses.IconUser).ToHtmlString();

这已经解决了我的问题,但我仍然不清楚为什么 helper.ViewContext.ViewDatahelper.ViewData 应该指向不同的实例,给定方式模拟已设置。

关于c# - 模拟 ViewContext 以测试验证错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17271688/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com