2011-09-06 61 views
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. 
     } 

当我接触到调用我无法访问该方法的方法。

任何人都知道如何做到这一点?

+1

可能看起来很可笑,但你添加的命名空间来扩展你在你的测试类? – Paul

+1

你不需要嘲笑任何东西。只是'HtmlHelper helper = null;'会让你得到你想要的 –

回答

0

尝试在顶部包含扩展名为using的命名空间。

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()); 
}