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

时间:2013-06-24 09:03:28

标签: c# asp.net-mvc validation

这是故事。为了能够将格式良好的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应该给我span一个field-validation-error类,并显示错误消息“必须分配字段”。在我的观察窗口中,html.ViewData.ModelState["Field"].Errors的计数为1.我必须遗漏一些东西。谁能看到它是什么?

1 个答案:

答案 0 :(得分:3)

我将测试修改为直接使用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应该指向不同的实例,考虑到模拟的设置方式。