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

本文关键字:错误 消息 验证 测试 ViewContext 模拟 | 更新日期: 2023-09-27 18:19:48

下面是故事。为了能够将格式良好的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>

方法本身并不难写。更困难的是单元测试。为了使我的扩展方法可测试,我需要使用适当的mock创建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应该会给我一个span,它的类为field-validation-error,并显示一条错误消息:"必须分配字段。"在我的监视窗口中,html.ViewData.ModelState["Field"].Errors的计数为1。我一定是错过了什么。有人能看到它是什么吗?

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

我修改了测试,直接使用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();

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