[個人筆記]如何產生HtmlTextWriter類別

摘要:[個人筆記]如何產生HtmlTextWriter類別

在網頁程式中會用到HtmlTextWriter這個物件於override的一些Method中,例如:

protected override void RenderPreText(HtmlTextWriter writer)

protected override void RenderPostText(HtmlTextWriter writer)

protected override void Render(HtmlTextWriter writer)

這些在網頁執行中會由系統傳物件進去,但是在單元測試的時候就需要自己組成傳入

但是我們執行單元測試的時候要怎麼樣組成一個HttpWriter呢?嘗試一下作法,完成之後筆記一下:

StringWriter tStringWriter = new StringWriter();
HtmlTextWriter tHtmlTextwriter = new HtmlTextWriter(tStringWriter);
tHtmlTextwriter.RenderBeginTag(HtmlTextWriterTag.Div);
tHtmlTextwriter.RenderEndTag();

Note:單元測試時如果對象有override TagKey,則使用Accessor物件仍無法測試>"<
Note:其實單純HtmlTextWriter tHtmlTextwriter = new HtmlTextWriter(new StringWriter());一樣可行..