BT

Writing Asynchronous Tests with Rx and Silverlight

by Jonathan Allen on Aug 09, 2011 |

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.

Hello stranger!

You need to Register an InfoQ account or 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

Educational Content

General Feedback
Bugs
Advertising
Editorial
InfoQ.com and all content copyright © 2006-2013 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT