领测软件测试网
为Asp.net控件写单元测试(ViewState)[2] 单元测试代码
// 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")]
|
文章来源于领测软件测试网 https://www.ltesting.net/
TAG:
asp
ASP
Asp
ViewState
单元
控件