Click here to Skip to main content
13,148,889 members (52,463 online)
Click here to Skip to main content
Add your own
alternative version

Stats

11.9K views
1 bookmarked
Posted 19 Dec 2011

Silverlight Testing Using async await Syntax

, 20 Dec 2011
Rate this:
Please Sign up or sign in to vote.
Silverlight testing using async await syntax

This is going to be my first blog post. I intend to blog a bit when I’ve something intresting to share about coding, mostly in C#. Recently, I came across an interesting post on the blog of ayende about testing in Silverlight. When testing code which makes asynchronous calls, Microsoft gave us the enqueuecallback method shown below:

1 [TestMethod]        
2 [Asynchronous]        
3 public void OldWay()        
4 {            
5    var something = SomeTestTask.DoSomethingAsync();            
6    EnqueueConditional((() => 7        something.IsCompleted || 
8        something.IsFaulted));            
9    EnqueueCallback(() => 10    {            
11        var another = SomeTestTask.DoSomethingAsync();                
12        EnqueueConditional((() => 13            another.IsCompleted || 
14            another.IsFaulted));                
15        EnqueueCallback(() => 16        {                    
17            EnqueueDelay(100);                    
18            Assert.AreEqual(42, another.Result);                    
19            EnqueueTestComplete();
20        });            
21    });        
22 }

Bennage helped out here as described in his blog post to allow us the following syntax:

1 [TestMethod]
2 [Asynchronous]        
3 public IEnumerable<Task> NewWay()        
4 {            
5    var something = SomeTestTask.DoSomethingAsync();
6 yield return something;            
7    var another = SomeTestTask.DoSomethingAsync();
8 yield return another;            
9 yield return Delay(100);            
10 Assert.AreEqual(42, another.Result);        
11 }

In my opinion, this is much more readable. Now, Microsoft has the new Async CTP library. We can however go one step further, by using the the async/await syntax. I’ve made a small modification to the library created by Bennage that allows the syntax below:

1 [TestMethod]
2 [Asynchronous]
3 public async Task AnotherWay()        
4 {
5    await SomeTestTask.DoSomethingAsync();            
6 int result = await SomeTestTask.DoSomethingAsync(); 
				7    await Delay(100);            
8    Assert.AreEqual(42, result);        
9 }

The code to do this can be found on github.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Marwijn
United States United States
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170924.2 | Last Updated 21 Dec 2011
Article Copyright 2011 by Marwijn
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid