Write unit test (viewstate) for Asp.net controls)

Source: Internet
Author: User

Generally, a typical Asp.net control stores at least some attributes with viewstate, so that you do not need to reset the attributes after page PostBack. In this article, I will introduce how to write unit tests for controls to ensure that an attribute is correctly saved in viewstate.

For demonstration, I wrote a simple control.

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 );
}
}
}

This control simply outputs its unique property text.

Okay. Let's write a simple unit test to ensure that this control works correctly.

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 shoshould 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 ),

"Shoshould have rendered the text ");
}
Private Static string getrenderedtext (control C ){
Htmltextwriter writer = new htmltextwriter (New stringwriter ());
C. rendercontrol (writer );
Return writer. innerwriter. tostring ();
}
}
}

It seems that we have covered 100% of the code, right? In fact, we cannot guarantee that the property of this control has been correctly stored in viewstate. However, we know that all functions related to viewstate are protected and cannot be accessed from outside. There are many ways to solve this problem. Here we write an 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 ();
}
Then let our controls implement it:

# Region iControl members
Void iControl. loadviewstate (Object savedstate ){
Loadviewstate (savedstate );
}
Object iControl. saveviewstate (){
Return saveviewstate ();
}
Void iControl. trackviewstate (){
Trackviewstate ();
}
# Endregion

Now we can test 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 = 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 ");
}

Note that our interface is internal. To make the test case accessible, you need to add

Using system. runtime. compilerservices;
[Assembly: internalsvisibleto ("mycontrollibrary. test")]

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.