• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
    • 軟件測試技術
    • 軟件測試博客
    • 軟件測試視頻
    • 開源軟件測試技術
    • 軟件測試論壇
    • 軟件測試沙龍
    • 軟件測試資料下載
    • 軟件測試雜志
    • 軟件測試人才招聘
      暫時沒有公告

    字號: | 推薦給好友 上一篇 | 下一篇

    為Asp.net控件寫單元測試(ViewState)

    發布: 2009-5-21 09:46 | 作者: 網絡轉載 | 來源: 測試時代采編 | 查看: 22次 | 進入軟件測試論壇討論

    領測軟件測試網 通常一個典型的asp.net控件至少會用ViewState存儲一些屬性,以便于在頁面postback后不用重新設置。在這篇文章里我將介紹如何為控件寫單元測試,以確保一個屬性被正確的保存在ViewState里。

    為了演示,我寫了一個簡單的控件。

    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); } } } 這個控件只是簡單的將它唯一的屬性Text輸出。

    好的,讓我們寫一個簡單的單元測試,以確保這個控件正確的工作。

    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>(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<string>(value, label.Text, "Property value isn't the same as what we set"); } [TestMethod] public void RenderEmpty() { NewLabel label = new NewLabel(); Assert.AreEqual<string>(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<string>(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(); } } } 看上去我們已經覆蓋了100%的代碼,是這樣嗎?事實上我們根本不能保證這個控件的屬性已經被正確的存儲到ViewState里了?墒俏覀冎琅cViewState有關的函數都是protected的,并不能從外部訪問。解決這個問題,可以有很多辦法,這里我們寫一個internal interface,
    // Interface to expose protected methods from // the Control class to our unit test internal interface IControl { void LoadViewState(object savedState); object SaveViewState(); void TrackViewState(); } 然后讓我們的控件去實現它:
    #region IControl Members void IControl.LoadViewState(object savedState) { LoadViewState(savedState); } object IControl.SaveViewState() { return SaveViewState(); } void IControl.TrackViewState() { TrackViewState(); } #endregion 現在就可以測試ViewState了:

    [TestMethod] public void TextSavedInViewState() { // Create the control, start tracking viewstate, // then set a new Text value const 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 state object viewState = ((IControl)label).SaveViewState(); // Create a new control instance and load the state // back into it, overriding any existing values NewLabel newLabel = new NewLabel(); label.Text = firstValue; <string>(secondValue, newLabel.Text, "Value restored from viewstate does not match the original value we set"); } 這里注意一點,我們的接口是internal的,為了讓測試用例可以訪問它,需要添加

    using System.Runtime.CompilerServices; [assembly: InternalsVisibleTo("MyControlLibrary.Test")]

    延伸閱讀

    文章來源于領測軟件測試網 http://www.kjueaiud.com/

    TAG: asp ASP Asp ViewState 單元 控件


    關于領測軟件測試網 | 領測軟件測試網合作伙伴 | 廣告服務 | 投稿指南 | 聯系我們 | 網站地圖 | 友情鏈接
    版權所有(C) 2003-2010 TestAge(領測軟件測試網)|領測國際科技(北京)有限公司|軟件測試工程師培訓網 All Rights Reserved
    北京市海淀區中關村南大街9號北京理工科技大廈1402室 京ICP備10010545號-5
    技術支持和業務聯系:info@testage.com.cn 電話:010-51297073

    軟件測試 | 領測國際ISTQBISTQB官網TMMiTMMi認證國際軟件測試工程師認證領測軟件測試網

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>