<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="RSS_xslt_style.asp" version="1.0" ?>
<rss version="2.0" xmlns:WebWizForums="http://syndication.webwiz.co.uk/rss_namespace/">
 <channel>
  <title>DevForce Community Forum : Silverlight Unit Test Example</title>
  <link>http://www.ideablade.com/forum/</link>
  <description>This is an XML content feed of; DevForce Community Forum : DevForce 2009 : Silverlight Unit Test Example</description>
  <pubDate>Tue, 28 Apr 2026 16:35:03 -700</pubDate>
  <lastBuildDate>Wed, 29 Jul 2009 15:37:04 -700</lastBuildDate>
  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  <generator>Web Wiz Forums 9.69</generator>
  <ttl>360</ttl>
  <WebWizForums:feedURL>www.ideablade.com/forum/RSS_post_feed.asp?TID=1343</WebWizForums:feedURL>
  <image>
   <title>DevForce Community Forum</title>
   <url>http://www.ideablade.com/forum/forum_images/IdeaBlade_logo_tm.png</url>
   <link>http://www.ideablade.com/forum/</link>
  </image>
  <item>
   <title>Silverlight Unit Test Example : Very nice! Thanks for posting....</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=1343&amp;PID=5063#5063</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=11" rel="nofollow">kimj</a><br /><strong>Subject:</strong> 1343<br /><strong>Posted:</strong> 29-Jul-2009 at 3:37pm<br /><br />Very nice!&nbsp; Thanks for posting.]]>
   </description>
   <pubDate>Wed, 29 Jul 2009 15:37:04 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=1343&amp;PID=5063#5063</guid>
  </item> 
  <item>
   <title>Silverlight Unit Test Example : Here is an example of a more advanced...</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=1343&amp;PID=5056#5056</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=308" rel="nofollow">skingaby</a><br /><strong>Subject:</strong> 1343<br /><strong>Posted:</strong> 29-Jul-2009 at 12:24pm<br /><br />Here is an example of a more advanced test.  This one uses the Ideablade provided AsyncParallelTask to run a bunch of queries to preload the Cache.  It also uses the AsyncSerialTask to create the chain of tasks.&nbsp;&nbsp;&nbsp;Whereas the above example shows the LoginAsync in one method and then the  LoginCallback, which in turn calls the ExecuteQueryAsync which calls back to the QueryCallback, the below example chains these as a set of tasks in an AsyncSerialTask and then calls the Execute method on the whole chain.  The unit tests assertions are sprinkled throughout the chain so they are executed in the right places.  Note how Task 7 actually encapsulates several lines of code that: a) depend on the results of the Async Queries above; and b) setup the IIndexPrice object that is needed for the IndexPriceService IsValidAsync and GetRateAsync method tests.<br /><br /><table width="99%"><tr><td><pre class="BBcode">&#091;TestMethod&#093;<br />//&#091;Ignore&#093;<br />&#091;Asynchronous&#093;<br />public void TestAsyncIndexPriceService()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_em = new DomainModel.DomainModelEntityManager();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var task1 = AsyncSerialTask.Create();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var task2 = task1.AddAsyncLogin(_em, new LoginCredential(null, null, null));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var task3 = task2.AddAction(x =&gt; Assert.IsTrue(_em.IsLoggedIn));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var task4 = task3.AddAction(x =&gt; Assert.IsTrue(_em.IsConnected));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var task5 = task4.AddAsyncAction&lt;AsyncEventArgs&gt;((loginEventArgs, callback) =&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_em.InitializeCacheAsync(callback,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TestAsyncIndexPriceServiceParallelCallback));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IIndexPrice index = null;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var task6 = task5.AddAsyncQuery(x =&gt; _em.Pipelines);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var task7 = task6.AddAction(x =&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var deal = Deal.Create(SampleData.SampleUser);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var price = deal.DealPricings&#091;0&#093;;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;deal.FlowDateStart = new DateTime(2009, 2, 10);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;deal.FlowDateEnd = new DateTime(2009, 2, 10);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;index = (IIndexPrice)price;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;index.Adder = 0M;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;index.HmlFlag = IndexHmlFlag.Medium;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;index.Pipeline = Pipeline.Fetch("BANANA");<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var task8 = task7.AddAsyncAction&lt;InvokeServerMethodEventArgs&gt;((serialArgs, callback) =&gt; IndexPriceService.IsValidAsync(null, index, callback));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var task9 = task8.AddAction(validArgs =&gt; Assert.AreEqual(true, Convert.ToBoolean(validArgs.Result)));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var task10 = task9.AddAsyncAction&lt;InvokeServerMethodEventArgs&gt;((args, callback) =&gt; IndexPriceService.GetRateAsync(null, index, callback));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var task11 = task10.AddAction(rateArgs =&gt; Assert.AreEqual(2M, Convert.ToDecimal(rateArgs.Result)));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EnqueueCallback(() =&gt; task9.Execute(null, args =&gt; TestAsyncIndexPriceServiceCallback(args)));<br />}<br /><br />//The following method is a workaround because the AsyncParallelTaskCompletedArgs do not inherit from AsyncEventArgs, which the AsyncSerialTask needs.<br />public void TestAsyncIndexPriceServiceParallelCallback(AsyncParallelTaskCompletedArgs args)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// We passed this callback as input parm into the AsyncParallelTask<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var serialCB = args.ExecutionInput as AsyncCompletedCallback&lt;AsyncEventArgs&gt;;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Build new args, taking advantage of userState input argument.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AsyncEventArgs newArgs = null;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (args.Ok)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// This is arbitrary - here we supply parallel task completion info ...<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newArgs = new AsyncEventArgs(args.CompletionMap);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Also arbitrary - if parallel task failed provide some info ...<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newArgs = new AsyncEventArgs(args.Errors&#091;0&#093;.Exception, false, args.Errors);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Now call the serial task's callback action.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;serialCB(newArgs);<br />}<br /><br />public void TestAsyncIndexPriceServiceCallback(AsyncSerialTaskCompletedArgs&lt;InvokeServerMethodEventArgs&gt; args)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EnqueueTestComplete();<br />}<br /><br />public static void InitializeCacheAsync(AsyncCompletedCallback&lt;AsyncEventArgs&gt; serialCallback, Action&lt;AsyncParallelTaskCompletedArgs&gt; completionAction)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var task = AsyncParallelTask.Create();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;task.AddExceptionHandler(InitializationExceptionHandler);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AddInitializationQueries(task);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;task.Execute(serialCallback, completionAction);<br />}<br /><br />private static void AddInitializationQueries(AsyncParallelTask&lt;object&gt; task)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;task.AddAsyncQuery(1, x =&gt; _defaultManager.AccountingMonths);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;task.AddAsyncQuery(2, x =&gt; _defaultManager.DealTypes);<br />}<br /><br />public static void InitializationExceptionHandler(AsyncParallelTaskExceptionArgs args)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//ToDo: log exception?<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw args.Exception;<br />}</pre></td></tr></table><span style="font-size:10px"><br /><br />Edited by skingaby - 29-Jul-2009 at 12:27pm</span>]]>
   </description>
   <pubDate>Wed, 29 Jul 2009 12:24:50 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=1343&amp;PID=5056#5056</guid>
  </item> 
  <item>
   <title>Silverlight Unit Test Example : In an effort to test my Client...</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=1343&amp;PID=4834#4834</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=308" rel="nofollow">skingaby</a><br /><strong>Subject:</strong> 1343<br /><strong>Posted:</strong> 23-Jun-2009 at 11:07am<br /><br />In an effort to test my Client Model, I created a Silverlight UnitTest project (<a href="http://code.msdn.microsoft.com/silverlightut" target="_blank">http://code.msdn.microsoft.com/silverlightut</a>).  A Google search for "silverlight unit test asynchronous" helped a lot too.<br /><br />Here are the steps I used:<br />1) I used the SilverlightUT template to create a new project.<br />2) I added a reference to the IdeaBlade.x.SL assemblies, the Ideablade sample Prism Explorer Infrastructure project (for the TypeHelper class) and to my Client Model project.<br />3) I went to the properties of my ShellWeb project (the host for the Silverlight apps) and went to the Silverlight Applications tab where I added the ClientModel.Test project and its new test container page.  <br />4) I added a unit test to the Test.cs file that the template created for me in step 1.<br />5) I spent a ridiculous amount of time debugging said unit test.<br />6) I finally did a little happy dance and some Matchitecture as a reward for getting it done.<br /><br />Here is a working sample of the test I ran to demonstrate connectivity from client to server:<br /><br /><table width="99%"><tr><td><pre class="BBcode">&#091;TestClass&#093;<br />public class Test : SilverlightTest<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;private DomainModel.DomainModelEntityManager _em = new DomainModel.DomainModelEntityManager();<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&#091;TestMethod&#093;<br />&nbsp;&nbsp;&nbsp;&nbsp;&#091;Asynchronous&#093;<br />&nbsp;&nbsp;&nbsp;&nbsp;public void LoadAllDeals()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; EnqueueCallback(() =&gt; _em.LoginAsync(null, args2 =&gt; LoginCallback(args2, null), null));<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;private void LoginCallback(LoginEventArgs args2, Action callback)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (args2.Error != null)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw args2.Error;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Assert.IsTrue(_em.IsLoggedIn);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Assert.IsTrue(_em.IsConnected);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _em.DefaultQueryStrategy = QueryStrategy.Normal;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; EnqueueCallback(() =&gt; _em.ExecuteQueryAsync(_em.Deals, args =&gt; QueryCallback(args, null), null));<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;private void QueryCallback(EntityFetchedEventArgs args, Action&lt;IEnumerable&gt; callback)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IList dls = TypeHelper.CreateObservableCollection(args.Result);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Assert.IsNotNull(dls);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Assert.IsTrue(dls.Count &gt; 0);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; EnqueueTestComplete();<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}</pre></td></tr></table><br /><br />Notice:<br />a) The Test class inherits from SilverlightTest<br />b) The test method LoadAllDeals has the &#091;Asynchronous&#093; attribute<br />c) Any call that needs a callback is done as a lambda expression inside the EnqueueCallback() method of the base SilverlightTest class<br />d) It was not necessary to Enqueue the Assertions<br />e) The last callback in the chain completes the chain by calling the EnqueueTestComplete() method. <span style="font-size:10px"><br /><br />Edited by skingaby - 29-Jul-2009 at 12:27pm</span>]]>
   </description>
   <pubDate>Tue, 23 Jun 2009 11:07:18 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=1343&amp;PID=4834#4834</guid>
  </item> 
 </channel>
</rss>