Writing Asynchronous Tests with Rx and Silverlight

| by Jonathan Allen Follow 495 Followers on Aug 09, 2011. Estimated reading time: 1 minute |

A note to our readers: You asked so we have developed a set of features that allow you to reduce the noise: you can get email and web notifications for topics you are interested in. Learn more about our new features.

Integration testing in Silverlight is hard. Pretty much any kind of external service is going to require an asynchronous call that returns on the UI thread. And unlike most unit testing frameworks, the one used for Silverlight runs on the same said thread so you cannot block it while waiting for the service call to return.

To make matters worse, the de facto testing framework for Silverlight, known simply as the Silverlight Unit Testing Framework, is very brittle. An unhandled exception on any thread will crash the entire test run, and as mentioned before, integration tests require multi-threading.

One solution to this is to always use EnqueueCallback to ensure that exceptions are properly reported to the unit testing framework. However this too can be difficult to get right and it only takes one mistake to disrupt all of the following tests. Richard Szalay is demonstrates how Reactive Extensions can be used to augment asynchronous testing while at the same time make it less error prone.

The techniques used by Richard assume that you are using the asynchronous pattern with the traditional Begin and End methods. The Observable.FromAsyncPattern function is used to wrap the service call in an IObservable. Once that is done the ObserveOnTest function attaches the unit test framework to the call chain via a specialized scheduler called the WorkItemTestScheduler. Finally the Subscribe function is called to kick off the sequence and evaluate the result.

Rate this Article

Adoption Stage

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Begin/End requirement by Richard Szalay

Rx doesn't require you use the begin/end pattern, that's just what WebRequest exposes (and the sample is a web request).

Rx can represent anything asynchronous, including completely custom sources. The code I posted on my blog simply marshals messages from any Rx source back to the MSTest stack so exceptions can be caught by it.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

1 Discuss

Login to InfoQ to interact with what matters most to you.

Recover your password...


Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.


More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.


Stay up-to-date

Set up your notifications and don't miss out on content that matters to you