A discussion about Rich Internet Applications, and user experience focusing on architecture, design patterns and methodologies.

Monday, November 9, 2009

Unit testing in Silverlight with Rhino

I try to follow the test driven design approach but I find it very difficult to do when ramping up on new technology.  Having wrapped up the initial release of Reflect (my MVVM core library),  I decided to try my hand at writing unit tests for Silverlight.

The first step was to get the Unit Testing Framework up and running.  The first step was installing the templates (see http://code.msdn.microsoft.com/silverlightut).  I struggled with this a bit, but it ended up being very simple.  Put the SilverlightTestClass_CSharp.zip file in C:\Users\Carlos\Documents\Visual Studio 2008\Templates\ItemTemplates\Visual C# and the SilverlightTestProject_CSharp.zip in C:\Users\Carlos\Documents\Visual Studio 2008\Templates\ProjectTemplates\Visual C# (or the VB files as required).  Reload Visual Studio and the templates show up:

image

image

If you place them in a subfolder called Silverlight, they will appear in the Silverlight category.  I have not figured out how to have them appear in both without duplicating the zip file.

When you create a new test project, you will need to replace the references to the testing libraries Microsoft.Silverlight.Testing and Microsoft.VisualStudio.QualityTools.UnitTesting.Silverlight.  Also, download Rhino for Silverlight here http://ayende.com/projects/rhino-mocks/downloads.aspx.  You will need to reference Castle.Core-Silverlight, Castle.DynamicProxy-Silverlight and Rhino.Mocks 3.5.Silverlight. 

From here on, its business as usual:

 

[TestMethod]
public void Navigator_goes_to_requested_page()
{
    Expect.Call(_Html.CurrentBookmark).Return(string.Empty);
    Expect.Call(delegate { _Html.NavigateToBookmark(ViewName); });
    _mock.ReplayAll();

    _navigator.RegisterPage<TestViewModelGeneric>(ViewName);
    _navigator.Goto(ViewName);

    _mock.VerifyAll();
    Assert.IsTrue(_RegionManager.Regions[RegionName].Views.Contains(_view));
    Assert.AreEqual(Visibility.Visible, ((View)_view).Visibility);
}

The framework runs the tests in a Silverlight App in a browser which is appropriate considering that is the environment you will probably be running your app, but I still haven’t wrapped my head around how I can run these tests for an continuous integration setup on a build server.

image

You can find the full source for Refract with unit tests here: http://refract.codeplex.com/.

Next Step

One of tests  runs synchronously on asynchronous code.  I was lucky that it passes, but it weighs on my conscience, so I will be using some of the built in test classes in the framework to write an asynchronous test.

No comments:

About Me

My photo
Toronto, ON, Canada
I am a technical architect at Navantis, with over 12 years experience in IT. I have been involved in various projects, including a Hospital information system called CCIS for which my team received the 2007 Tech-Net innovation award. I have been working with Silverlight since beta 1, and am very keen on practically applying this technology and WPF to line of business applications.