In an effort to test my Client Model, I created a Silverlight UnitTest project (
http://code.msdn.microsoft.com/silverlightut). A Google search for "silverlight unit test asynchronous" helped a lot too.
Here are the steps I used:
1) I used the SilverlightUT template to create a new project.
2) I added a reference to the IdeaBlade.x.SL assemblies, the Ideablade sample Prism Explorer Infrastructure project (for the TypeHelper class) and to my Client Model project.
3) I went to the properties of my ShellWeb project (the host for the Silverlight apps) and went to the Silverlight Applications tab where I added the ClientModel.Test project and its new test container page.
4) I added a unit test to the Test.cs file that the template created for me in step 1.
5) I spent a ridiculous amount of time debugging said unit test.
6) I finally did a little happy dance and some Matchitecture as a reward for getting it done.
Here is a working sample of the test I ran to demonstrate connectivity from client to server:
[TestClass]
public class Test : SilverlightTest
{
private DomainModel.DomainModelEntityManager _em = new DomainModel.DomainModelEntityManager();
[TestMethod]
[Asynchronous]
public void LoadAllDeals()
{
EnqueueCallback(() => _em.LoginAsync(null, args2 => LoginCallback(args2, null), null));
}
private void LoginCallback(LoginEventArgs args2, Action callback)
{
if (args2.Error != null)
throw args2.Error;
Assert.IsTrue(_em.IsLoggedIn);
Assert.IsTrue(_em.IsConnected);
_em.DefaultQueryStrategy = QueryStrategy.Normal;
EnqueueCallback(() => _em.ExecuteQueryAsync(_em.Deals, args => QueryCallback(args, null), null));
}
private void QueryCallback(EntityFetchedEventArgs args, Action<IEnumerable> callback)
{
IList dls = TypeHelper.CreateObservableCollection(args.Result);
Assert.IsNotNull(dls);
Assert.IsTrue(dls.Count > 0);
EnqueueTestComplete();
}
} |
Notice:
a) The Test class inherits from SilverlightTest
b) The test method LoadAllDeals has the [Asynchronous] attribute
c) Any call that needs a callback is done as a lambda expression inside the EnqueueCallback() method of the base SilverlightTest class
d) It was not necessary to Enqueue the Assertions
e) The last callback in the chain completes the chain by calling the EnqueueTestComplete() method.
Edited by skingaby - 29-Jul-2009 at 12:27pm