通常一個(gè)典型的asp.net控件至少會(huì)用ViewState存儲(chǔ)一些屬性,以便于在頁(yè)面postback后不用重新設(shè)置。在這篇文章里,北京北大青鳥校區(qū)將介紹如何為控件寫單元測(cè)試,以確保一個(gè)屬性被正確的保存在ViewState里。
為了演示,北京北大青鳥校區(qū)老師先寫了一個(gè)簡(jiǎn)單的控件。
namespace Eilon.Sample.Controls {using System;using System.Web.UI;public class NewLabel : Control {public string Text {get {string s = ViewState[Text] as string;return s ?? String.Empty;}set {ViewState[Text] = value;}}protected override void Render(HtmlTextWriter writer) {writer.Write(Text);}}}
這個(gè)控件只是簡(jiǎn)單的將它唯一的屬性Text輸出。
下面,北京北大青鳥校區(qū)老師寫一個(gè)簡(jiǎn)單的單元測(cè)試,以確保這個(gè)控件正確的工作。
namespace Eilon.Sample.Controls.Test {using System;using System.IO;using System.Web.UI;using Microsoft.VisualStudio.TestTools.UnitTesting;[TestClass]public class NewLabelTest {[TestMethod]public void TextReturnsEmptyStringDefault() {NewLabel label = new NewLabel();Assert.AreEqual(String.Empty, label.Text,Default text should be empty string (not null));}[TestMethod]public void GetSetText() {const string value = Some Text;NewLabel label = new NewLabel();label.Text = value;Assert.AreEqual(value, label.Text,Property value isn't the same as what we set);}[TestMethod]public void RenderEmpty() {NewLabel label = new NewLabel();Assert.AreEqual(String.Empty, GetRenderedText(label),Shouldn't have rendered anything);}[TestMethod]public void RenderWithText() {const string value = Some Text;NewLabel label = new NewLabel();label.Text = value;Assert.AreEqual(value, GetRenderedText(label),Should have rendered the text);}private static string GetRenderedText(Control c) {HtmlTextWriter writer = new HtmlTextWriter(new StringWriter());c.RenderControl(writer);return writer.InnerWriter.ToString();}}}
看上去已經(jīng)覆蓋了100%的代碼,是這樣嗎?事實(shí)上我們根本不能保證這個(gè)控件的屬性已經(jīng)被正確的存儲(chǔ)到ViewState里。可是我們知道與ViewState有關(guān)的函數(shù)都是protected的,并不能從外部訪問(wèn)。解決這個(gè)問(wèn)題,可以有很多辦法,這里我們寫一個(gè)internal interface,
// Interface to expose protected methods from// the Control class to our unit testinternal interface IControl {void LoadViewState(object savedState);object SaveViewState();void TrackViewState();}
然后讓我們的控件去實(shí)現(xiàn)它:
#region IControl Membersvoid IControl.LoadViewState(object savedState) {LoadViewState(savedState);}object IControl.SaveViewState() {return SaveViewState();}void IControl.TrackViewState() {TrackViewState();}#endregion
現(xiàn)在就可以測(cè)試ViewState了:
[TestMethod]public void TextSavedInViewState() {// Create the control, start tracking viewstate,// then set a new Text valueconst string firstValue = Some Text;const string secondValue = ViewState Text;NewLabel label = new NewLabel();label.Text = firstValue;((IControl)label).TrackViewState();label.Text = secondValue;// Save the control's stateobject viewState = ((IControl)label).SaveViewState();// Create a new control instance and load the state// back into it, overriding any existing valuesNewLabel newLabel = new NewLabel();label.Text = firstValue;((IControl)newLabel).LoadViewState(viewState);Assert.AreEqual(secondValue, newLabel.Text,Value restored from viewstate does not match the original value we set);}
這里注意一點(diǎn),我們的接口是internal的,為了讓測(cè)試用例可以訪問(wèn)它,需要添加
using System.Runtime.CompilerServices;[assembly: InternalsVisibleTo(MyControlLibrary.Test)]
文章來(lái)源:北京北大青鳥校區(qū)學(xué)術(shù)部