0
我需要測試像@Html.CustomLabel
和@Html.CustomLabelFor(m=>m.UserName)
這樣的自定義Html輔助方法。如何在Fack中添加視圖模型HttpContext
第一個比較容易。我有:
public static HtmlHelper Create()
{
var vc = new ViewContext {HttpContext = new FakeHttpContext()};
var html = new HtmlHelper(vc, new FakeViewDataContainer());
return html;
}
private class FakeHttpContext : HttpContextBase
{
private readonly Dictionary<object, object> items = new Dictionary<object, object>();
public override IDictionary Items
{
get { return items; }
}
}
private class FakeViewDataContainer : IViewDataContainer
{
private ViewDataDictionary viewData = new ViewDataDictionary();
public ViewDataDictionary ViewData
{
get{return viewData;}
set { viewData = value; }
}
}
但是如何寫第二個?我需要在HttpContext
中注入一個視圖模型來編寫測試。
public static HtmlHelper Create<T>()
{
var vc = new ViewContext {HttpContext = new FakeHttpContext()};
var html = new HtmlHelper(vc, new FakeViewDataContainer());
return html;
}
我應該怎麼做,以包括視圖模型T
的方法Create<T>
?