0
在我的其他问题之一,我得到了一个helpermethod这个真正伟大的答案:MVC单元测试单选助手扩展
using System;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
public static class RadioExtensions
{
public static IHtmlString MyRadioButtonFor<TModel, TProperty>(
this HtmlHelper<TModel> html,
Expression<Func<TModel, TProperty>> ex,
object value
)
{
var isAuthenticated = html.ViewContext.HttpContext.User.Identity.IsAuthenticated;
if (isAuthenticated)
{
return html.RadioButtonFor(ex, value);
}
return html.RadioButtonFor(ex, value, new { @disabled = "disabled" });
}
}
反正我试图单元测试这一点:
[Test]
public void RadioButtonHelper()
{
var cc = new Mock<ControllerContext>(
new Mock<HttpContextBase>(),
new RouteData(),
new Mock<ControllerBase>());
var mockViewContext = new Mock<ViewContext>(
cc,
new Mock<IView>(),
new TempDataDictionary());
var mockViewDataContainer = new Mock<IViewDataContainer>();
HtmlHelper helper = new HtmlHelper(mockViewContext.Object, mockViewDataContainer.Object);
helper.
}
当我接触到调用我无法访问该方法的方法。
任何人都知道如何做到这一点?
可能看起来很可笑,但你添加的命名空间来扩展你在你的测试类? – Paul
你不需要嘲笑任何东西。只是'HtmlHelper helper = null;'会让你得到你想要的 –