The Silverlight unit test framework provides support for asynchronous testing over the past few days. The meaning of asynchronous testing in this framework is to queue some additional tasks and execute them later. For example, if a request () method is asynchronous, the returned results cannot be directly tested after the method is called. However, in the test method testrequest (), the request () method is called () then, you can add some additional tasks to a queue and execute these tasks after exiting testrequest. Some Interfaces,You will understand:
Generally, the following four methods are used to add tasks to the queue:
Enqueuetestcomplete ()-Add a testcomplete () to the queue. This method tells the framework test is complete.
Enqueuecallback ()-Add a task to the queue
Enqueueconditional ()-Add a condition to judge the queue. If it is true, the execution will continue.
Enqueuedelay ()-add some waiting time to the queue
Demonstrate how to use it:
Return to the project to see how to do it:
1. Add a mocksinaservice:
Code
Public Class Mocksinaservice: isinaservice
{
Public List < Status > Statuslist { Get ; Set ;}
Public Void Getstatuses (system. Action < Ienumerable < Status > Ongetstatusescompleted = Null , System. Action < System. Exception > Onerror = Null , System. Action onfinally = Null )
{
Dispatchertimer Timer = New Dispatchertimer ();
Timer. Interval = Timespan. fromseconds ( 2 );
Timer. tick + = Delegate ( Object Sender, eventargs E)
{
Status teststatus =
New Status
{
Text = " This is test status " ,
Createdat = Datetime. Now
};
Ongetstatusescompleted ( New List < Status > () {Teststatus });
Timer. Stop ();
};
Timer. Start ();
}
}
This class is used to simulate sinaservice and reduce the dependency of mainpageviewmodel on it. In practice, sinaservice will call WebClient to obtain data asynchronously, but mainpageviewmodel does not actually care about how the data comes from. Adding this mock will facilitate unit testing. Note that the timer is used in the implementation to simulate asynchronous WebClient behavior.
2. Add a public property sinaservice to mainpageviewmodel, so that the test project can point it to the Mock class.
Code
Public Isinaservice sinaservice
{
Get
{
If (_ Sinaservice = Null )
{
_ Sinaservice = New Sinaservice ();
}
Return _ Sinaservice;
}
Set
{
If (Value ! = _ Sinaservice)
{
_ Sinaservice = Value;
}
}
}
3. Add the refreshhomelist () method to mainpageviewmodel.
Code
Public Void Refreshhomelist ()
{
Trace. detailmsg ( " Refreshhomelist " );
Ishomerefreshing = True ;
Sinaservice. getstatuses (
Delegate (Ienumerable < Status > Statuses)
{
Ishomerefreshing = False ;
Foreach (Status In Statuses)
{
Homelist. Add (Status );
}
},
Delegate (Exception)
{
Ishomerefreshing = False ;
});
}
4. Modify testinitialize as follows and implant the mock class.
Code
[Testinitialize]
Public VoidInitialize ()
{
_ Mainviewmodel= NewMainpageviewmodel ();
_ Mocksinaservice= NewMocksinaservice ();
_ Mainviewmodel. sinaservice=_ Mocksinaservice;
}
5. Add a Test method:
Code
[Testmethod]
[Asynchronous]
Public Void Refresh_homelist_success ()
{
Bool Ishomelistrefreshed = False ;
_ Mainviewmodel. homelist. collectionchanged + =
(S, E) =>
{
Ishomelistrefreshed = True ;
Assert. areequal (policycollectionchangedaction. Add, E. Action );
Assert. areequal ( 1 , E. newitems. Count, " Only shocould be + 1 item " );
};
_ Mainviewmodel. refreshhomelist ();
Enqueueconditional (() => Ishomelistrefreshed );
Enqueuecallback (() => Assert. areequal (_ mainviewmodel. homelist. Count, 1 , " Expected non-empty products list. " ));
Enqueuetestcomplete ();
Note the asynchronous keyword here, and make the mainpageviewmodeltests class inherit silverlighttest.
The test result is as follows:
References:
Silverlight2-unit-testing by Jeff Wilcox
2. asynchronous test support-Silverlight unit test framework and the UI thread by Jeff Wilcox
3. Silverlight unit testing, rhinomocks, unity and resharper by Justin angel