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