Since mvvm and TDD are used, the two tools are essential:
1. mvvm light Toolkit
2. Silverlight unit testing framework
The corresponding WP7 version is available. Add the DLL to the project.
The following are some important points:
1. Create a new project weibo7
2. Add a test project weibo7.test
1) Add Microsoft. Silverlight. Testing & Microsoft. visualstudio. qualitytools. unittesting. Silverlight to the project reference.
2) Modify mainpage () as follows:
Code
// Constructor
Public Mainpage ()
{
Initializecomponent ();
// Set up Unit Testing
Content = Unittestsystem. createtestpage ();
Imobiletestpage imtp = Content As Imobiletestpage;
If (Imtp ! = Null )
{
Backkeypress + = (X, Xe) => Xe. Cancel = Imtp. navigateback ();
}
}
3) create a new class mainviewmodeltests
Code
Using System;
Using System. net;
Using Microsoft. Silverlight. testing;
Using Microsoft. visualstudio. testtools. unittesting;
Using Weibo7.viewmodels;
Namespace Weibo7.test
{
[Testclass]
Public Class Mainviewmodeltests: silverlighttest
{
}
}
4) Add a new test method.
Code
[Testmethod]
Public Void Ishomerefreshing_setvalue_raisespropertychanged ()
{
VaR mainviewmodel = New Mainviewmodel ();
Bool Propchanged = False ;
Mainviewmodel. propertychanged + =
(S, E) =>
{
If (E. propertyname = " Ishomerefreshing " )
{
Propchanged = True ;
}
};
Mainviewmodel. ishomerefreshing = ! Mainviewmodel. ishomerefreshing;
Assert. istrue (propchanged );
}
3. Add a viewmodelbase class from which all viewmodels must inherit
Code
Using System. componentmodel;
Namespace Weibo7.viewmodels
{
Public Class Viewmodelbase
{
Protected Void Onpolicypropertychanged ( String P)
{
If (Propertychanged ! = Null )
{
Propertychanged ( This , New Propertychangedeventargs (p ));
}
}
Public Event Propertychangedeventhandler propertychanged;
}
}
4. Add the mainviewmodel class in weibo7 and add an ishomerefreshing attribute. This is used to bind the progressbar. Currently, the UI has not been created, but it can be tested!
Code
Private Bool _ Ishomerefreshing = False ;
/// <Summary>
/// Indicates whether there is an indeterminate operation in progress
/// </Summary>
Public Bool Ishomerefreshing
{
Get { Return _ Ishomerefreshing ;}
Set
{
If (Value ! = _ Ishomerefreshing)
{
_ Ishomerefreshing = Value;
Onpolicypropertychanged ( " Ishomerefreshing " );
}
}
}
If there is no problem, run the test project and you will see the following results:
More detailed results: