2011-09-06 62 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()); 
}