惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

美团技术团队
P
Proofpoint News Feed
WordPress大学
WordPress大学
P
Proofpoint News Feed
宝玉的分享
宝玉的分享
腾讯CDC
罗磊的独立博客
P
Privacy International News Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Latest news
Latest news
C
Cisco Blogs
Project Zero
Project Zero
Apple Machine Learning Research
Apple Machine Learning Research
The GitHub Blog
The GitHub Blog
博客园 - 叶小钗
Spread Privacy
Spread Privacy
博客园 - 三生石上(FineUI控件)
Blog — PlanetScale
Blog — PlanetScale
C
Check Point Blog
博客园 - 聂微东
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Tenable Blog
Know Your Adversary
Know Your Adversary
Security Latest
Security Latest
T
The Blog of Author Tim Ferriss
云风的 BLOG
云风的 BLOG
L
Lohrmann on Cybersecurity
IT之家
IT之家
T
Tor Project blog
S
SegmentFault 最新的问题
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
月光博客
月光博客
L
LINUX DO - 热门话题
C
CERT Recently Published Vulnerability Notes
T
Threat Research - Cisco Blogs
A
Arctic Wolf
D
DataBreaches.Net
C
Cybersecurity and Infrastructure Security Agency CISA
博客园 - 【当耐特】
T
The Exploit Database - CXSecurity.com
T
Threatpost
Hacker News - Newest:
Hacker News - Newest: "LLM"
Scott Helme
Scott Helme
博客园 - 司徒正美
Vercel News
Vercel News
H
Help Net Security
N
News and Events Feed by Topic
MyScale Blog
MyScale Blog
Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy & Cybersecurity Law Blog

博客园 - 心利

DocOptimizer 0.9.0 Beta Released SharePoint GroupedItemPicker Control Drag & Drop between SharePoint Document libraries Improving SharePoint User Experience With JQuery-Client Side Form Validate SharePoint How To:定位错误:“An SPRequest object was not disposed .." SharePoint Tips and Tricks --如何用JS向PeopleEditor填充数据 SharePoint Tips and Tricks -- Ribbon,People Editor 使用T4为数据库自动生成实体类(C#) (Tips&Tricks)用客户端模板精简JavaScript代码 (Tips &Tricks)如何为Windows Mobile 创建拨号连接--C# System.Xml FAQ Part 1 Windows Ce vs Windows Mobile Asp.net程序中生成Excel报表 My Page StartKit项目概览 做一个更好的程序员 .Net类库中实现的HashTable 使用Control Adapters优化Asp.net控件 ViewState 简述一(With Example And Apply to Asp.net) 桌面背景:rss新闻阅读器(图)
为Asp.net控件写单元测试(ViewState)
心利 · 2007-06-05 · via 博客园 - 心利
 

通常一个典型的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里了。可是我们知道与ViewState有关的函数都是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;
((IControl)newLabel).LoadViewState(viewState);
Assert.AreEqual<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")]