• <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-04-10來源:作者:點擊數: 標簽:單元ViewStateAspASP控件
    通常一個典型的asp .net 控件至少會用ViewState存儲一些屬性,以便于在頁面postback后不用重新設置。在這篇文章里我將介紹如何為控件寫 單元測試 ,以確保一個屬性被正確的保存在ViewState里。 為了演示,我寫了一個簡單的控件。 namespace Eilon.Sample.Contr
     通常一個典型的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.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();
    }
    }
    }

    原文轉自:http://www.kjueaiud.com

    老湿亚洲永久精品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>