.NET
Async tests in Silverlight
One of the things that we do is build a lot of stuff in Silverlight, usually, those things are either libraries or UI. Testing Silverlight was always a problem, but at least there is a solution OOTB for that.
Unfortunately, the moment that you start talking about async tests (for example, you want to run a web server to check things), you need to do things like this, EnqueueCallback, EnqueueConditional and other stuff that makes the test nearly impossible to read.
Luckily for us, Christopher Bennage stopped here for a while and created a solution.
It allows you to take the following sync test:
[Fact]
public void CanUpload()
{
var ms = new MemoryStream();
var streamWriter = new StreamWriter(ms);
var expected = new string('a',1024);
streamWriter.Write(expected);
streamWriter.Flush();
ms.Position = 0;
var client = NewClient();
client.UploadAsync("abc.txt", ms).Wait();
var downloadString = webClient.DownloadString("/files/abc.txt");
Assert.Equal(expected, downloadString);
}
And translate it to:
[Asynchronous]
public IEnumerable<Task> CanUpload()
{
var ms = new MemoryStream();
var streamWriter = new StreamWriter(ms);
var expected = new string('a', 1024);
streamWriter.Write(expected);
streamWriter.Flush();
ms.Position = 0;
yield return client.UploadAsync("abc.txt", ms);
var async = webClient.DownloadStringTaskAsync("/files/abc.txt");
yield return async;
Assert.AreEqual(expected, async.Result);
}
It makes things so much easier. To set this us, just reference the project and add the following in the App.xaml.cs file:
private void Application_Startup(object sender, StartupEventArgs e)
{
UnitTestSystem.RegisterUnitTestProvider(new RavenCustomProvider());
RootVisual = UnitTestSystem.CreateTestPage();
}
And you get tests that are now easy to write and run in Silverlight.
Reference: Async tests in Silverlight from our NCG partner Oren Eini at the Ayende @ Rahien blog.