在我的另一个问题中,我得到了一个非常好的答案:
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.
}
当我到达调用方法的部分时,我无法到达该方法。
有人知道怎么做吗?
尝试在顶部将命名空间作为using
包含到扩展中。
要测试静态帮助器,只需执行以下操作
[Test]
public void MyHelperTests()
{
HtmlHelper html = null;
var result = html.MyRadioButtonFor(/* your params */);
Assert.IsInstanceOfType(typeof(MvcHtmlString), result);
Assert.AreEqual(/* your results */, result.ToString());
}