This post is intended to show how to create unit tests on RIA Services. Step by step, I will show how to adapt the template “Silverlight Unit Test Application” in order to test its RIAs Services. To do this I will use my project “NorthwindDomainDriven” which is detailed in the post:Domain Driven Architecture with Entity Framework 4.0 (POCO), RIA services and silverlight.
How to create the unit test step by step?
First we must create the project that will contain unit tests. To do this we will create an “Silverlight Unit Test Application”:

Do not forget to check “Enable. NET RIA Services”
After you must write the test class, and make asynchronous calls to the RIA service that we want to test. To write an asynchronous test you need to derive from this « SilverlightTest” class, as well as mark your method with an Asynchronous attribute. By deriving from the “SilverlightTest” class you get access to several "EnqueueXXX" methods. These methods let you "queue" up code blocks (delegates) with the code you want to execute asynchronous.
[TestClass]
public class TestCategoryService : SilverlightTest
{
//...Test code here...//
}
To test the service, I decide to make a classic CRUD. The sequence of asynchronous calls that I have defined:
1. Initialisation du contexte
2. Récupération des éléments en DB
3. Création d’un nouvel élément
4. Récupération des éléments en DB
5. Update de l’élément ajouté
6. Suppression de l’élément ajouté
Here is the code:
[TestClass]
public class TestCategoryService : SilverlightTest
{
private CategoryDomainContext context;
private SubmitOperation submitInsertOperation;
private SubmitOperation submitUpdateOperation;
private SubmitOperation submitDeleteOperation;
[TestInitialize]
public void InitContext()
{
context = new CategoryDomainContext();
}
[TestMethod]
[Asynchronous] // Tell that it's an asynchronous method
[Description("Test the Create, Update, Retrieve and Delete methods. ")]
public void TestCategoryServiceCRUD()
{
try
{
//Reteieve
var loadOperation = context.Load(context.GetCategoriesQuery());
loadOperation.Completed += new EventHandler(loadOperation_Completed);
//Retrieve
EnqueueConditional(() => loadOperation.IsComplete); //Will not execute until the load operation complete
EnqueueCallback(() => Assert.AreEqual(context.CategoryDTOs.Count, 9));
//Insert
EnqueueConditional(() => submitInsertOperation != null);
EnqueueConditional(() => submitInsertOperation.IsComplete);
EnqueueCallback(() => Assert.IsTrue(true));
//Update
EnqueueConditional(() => submitUpdateOperation != null);
EnqueueConditional(() => submitUpdateOperation.IsComplete);
EnqueueCallback(() => Assert.IsTrue(true));
//Delete
EnqueueConditional(() => submitDeleteOperation != null);
EnqueueConditional(() => submitDeleteOperation.IsComplete);
EnqueueCallback(() => Assert.IsTrue(true));
}
catch (Exception ex)
{
Assert.Fail(ex.Message);
}
finally
{
EnqueueTestComplete();
}
}
void loadOperation_Completed(object sender, EventArgs e)
{
//Insert category
CategoryDTO categoryDTO = new CategoryDTO()
{
CategoryName = "MyTest",
Description = "My test category",
Picture = null
};
context.CategoryDTOs.Add(categoryDTO);
SubmitOperation submitInsertOperation = context.SubmitChanges();
submitInsertOperation.Completed += new EventHandler(submitInsertOperation_Completed);
}
void submitInsertOperation_Completed(object sender, EventArgs e)
{
//Reload context
context = new CategoryDomainContext();
var loadOperation = context.Load(context.GetCategoriesQuery());
loadOperation.Completed += new EventHandler(reloadOperation_Completed);
}
void reloadOperation_Completed(object sender, EventArgs e)
{
//Update category
CategoryDTO categoryDTO = context.CategoryDTOs.First(c => c.CategoryName == "MyTest");
categoryDTO.CategoryName = "MyTest2";
submitUpdateOperation = context.SubmitChanges();
submitUpdateOperation.Completed += new EventHandler(submitUpdateOperation_Completed);
}
void submitUpdateOperation_Completed(object sender, EventArgs e)
{
//Delete category
context.CategoryDTOs.Remove(context.CategoryDTOs.FirstOrDefault(c => c.CategoryName == "MyTest2"));
submitDeleteOperation = context.SubmitChanges();
}
}
Here is how to test an RIA service. To test a complete Silverlight application I recommend this article:
http://jonas.follesoe.no/CommentView,guid,f2fc834b-704e-40ae-9684-44cfa8096bed.aspx
Here is the complete code of the article: