安全中国首页 > 编程中心 > .NET编程
 
安全中国网友投稿专用上传FTP空间:
Ftp服务器:download.anqn.com
Ftp端口:21
用户名:anqn
密 码:anqn.com
 

为Asp.net控件写单元测试(ViewState)[2]

更新时间:2008-7-29 15:13:59
责任编辑:ShellExp
热 点:

// 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(secondValue, newLabel.Text,
"Value restored from viewstate does not match the original value we set");
}

  这里注意一点,我们的接口是internal的,为了让测试用例可以访问它,需要添加

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


 
学习软件编程开发技术,推荐加入以下软件编程培训班:
易语言软件编程培训班(简单易学)  Delphi软件编程培训班  VC++软件编程培训班
VB软件编程培训班  JAVA高端编程就业研发班

学习网站开发制作技术,推荐加入以下网站开发培训班:
ASP.net网站开发项目实战班  ASP语言网站建设培训班

学习网络安全入侵防护技术,推荐加入以下技术培训班:
大型网络安全入侵防护班  网站脚本程序全方位安全检测班

学习网络管理、网吧运营维护技术(网管),推荐加入以下培训班:
大型网吧技术管理人才特训班  Linux网络嵌入架构工程师培训班

学习专项特殊技术,推荐加入以下专项技术培训班:
软件与游戏外挂脱壳破解班(逆向工程)  赚钱王道-网赚技能培训班  Flash动画设计师就业特训班

 
相关编程
一日一文章
 
一日一软件
一日一动画