<?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 testing and cocktail 2 beta</title>
  <link>http://www.ideablade.com/forum/</link>
  <description>This is an XML content feed of; DevForce Community Forum : Community Forum : Silverlight Unit testing and cocktail 2 beta</description>
  <pubDate>Sat, 11 Apr 2026 02:04:13 -700</pubDate>
  <lastBuildDate>Fri, 02 Nov 2012 10:10:53 -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=3758</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 testing and cocktail 2 beta :  Walid,Making MefCompositionProvider...</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=3758&amp;PID=15063#15063</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=1005" rel="nofollow">mgood</a><br /><strong>Subject:</strong> 3758<br /><strong>Posted:</strong> 02-Nov-2012 at 10:10am<br /><br />Walid,<div>Making MefCompositionProvider public would be the wrong thing after thinking about it again. Part of a good unit test is knowing exactly what the state is at the beginning of the test. It's hard to do that with MEF, because of the discovery mechanisms&nbsp;inherent&nbsp;to MEF. MEF will scoop up all your exports in the test project and after a while it becomes nearly impossible to control what's in the container leading to tests failing for unknown reasons or worse succeeding when they should fail.</div><div> </div><div><br></div><div>Therefore at the beginning of each test, composition should be configured with a tightly controlled provider like so.&nbsp;</div><div><br></div><div><div>&nbsp; &nbsp; &nbsp; &nbsp; &#091;TestInitialize&#093;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; public void TestInitialize()</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var provider = new TestCompositionProvider();</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; provider.AddOrUpdateInstance&lt;IEventAggregator&gt;(new EventAggregator());</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Composition.SetProvider(provider);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div></div><div><br></div><div>A possible implementation for TestCompositionProvider could look like follows. I will document this in the DRC as well.</div><div><br></div><div><div>&nbsp; &nbsp; public class TestCompositionProvider : ICompositionProvider</div><div>&nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; private readonly Dictionary&lt;Type, object&gt; _container;</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; public TestCompositionProvider()</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _container = new Dictionary&lt;Type, object&gt;(); &nbsp; &nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; public void AddOrUpdateInstance&lt;T&gt;(T instance)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _container&#091;typeof(T)&#093; = instance;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;summary&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// Returns a lazy instance of the specified type.</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;/summary&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;typeparam name="T"&gt;Type of the requested instance. &lt;/typeparam&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; public Lazy&lt;T&gt; GetInstance&lt;T&gt;() where T : class</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new Lazy&lt;T&gt;(() =&gt; (T) _container&#091;typeof(T)&#093;);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;summary&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// Returns an instance of the specified type.</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;/summary&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;typeparam name="T"&gt;Type of the requested instance. &lt;/typeparam&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;returns&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// Null if instance is not present in the container.&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;/returns&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; public T TryGetInstance&lt;T&gt;() where T : class</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; object instance;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!_container.TryGetValue(typeof(T), out instance))</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (T) instance;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;summary&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// Returns all instances of the specified type.</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;/summary&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;typeparam name="T"&gt;Type of the requested instances. &lt;/typeparam&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; public IEnumerable&lt;T&gt; GetInstances&lt;T&gt;() where T : class</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return _container.Keys.Where(x =&gt; x == typeof(T)).Select(x =&gt; _container&#091;x&#093;).OfType&lt;T&gt;();</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;summary&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// Returns a lazy instance that matches the specified name and type.</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;/summary&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;param name="serviceType"&gt;The type to match.&lt;/param&gt;&lt;param name="contractName"&gt;The name to match.&lt;/param&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; public Lazy&lt;object&gt; GetInstance(Type serviceType, string contractName)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new Lazy&lt;object&gt;(() =&gt; _container&#091;serviceType&#093;);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;summary&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// Returns an instance that matches the specified name and type.</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;/summary&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;param name="serviceType"&gt;The type to match.&lt;/param&gt;&lt;param name="contractName"&gt;The name to match.&lt;/param&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;returns&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// Null if instance is not present in the container.&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;/returns&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; public object TryGetInstance(Type serviceType, string contractName)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; object instance;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!_container.TryGetValue(serviceType, out instance))</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return instance;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;summary&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// Returns all instances that match the specified name and type.</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;/summary&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;param name="serviceType"&gt;The type to match.&lt;/param&gt;&lt;param name="contractName"&gt;The name to match.&lt;/param&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; public IEnumerable&lt;object&gt; GetInstances(Type serviceType, string contractName)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return _container.Keys.Where(x =&gt; x == serviceType).Select(x =&gt; _container&#091;x&#093;);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;summary&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// Returns a factory that creates new instances of the specified type.</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;/summary&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;typeparam name="T"&gt;Type of instance the factory creates. &lt;/typeparam&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; public ICompositionFactory&lt;T&gt; GetInstanceFactory&lt;T&gt;() where T : class</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new ActivatorFactory&lt;T&gt;();</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;summary&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// Returns a factory that creates new instances of the specified type.</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;/summary&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;typeparam name="T"&gt;Type of instance the factory creates. &lt;/typeparam&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;returns&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// Null if the container cannot provide a factory for the specified type.&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;/returns&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; public ICompositionFactory&lt;T&gt; TryGetInstanceFactory&lt;T&gt;() where T : class</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return GetInstanceFactory&lt;T&gt;();</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;summary&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// Manually performs property dependency injection on the provided instance.</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;/summary&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;param name="instance"&gt;The instance needing property injection. &lt;/param&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; public void BuildUp(object instance)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Noop</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; }</div><div><br></div><div>&nbsp; &nbsp; public class ActivatorFactory&lt;T&gt; : ICompositionFactory&lt;T&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; where T : class</div><div>&nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;summary&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// Creates new instance.</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;/summary&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; public T NewInstance()</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Activator.CreateInstance&lt;T&gt;();</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; }</div></div><div><br></div><div> </div><div></div>]]>
   </description>
   <pubDate>Fri, 02 Nov 2012 10:10:53 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=3758&amp;PID=15063#15063</guid>
  </item> 
  <item>
   <title>Silverlight Unit testing and cocktail 2 beta : it doesn&amp;#039;t looks like Faking...</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=3758&amp;PID=15054#15054</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=998" rel="nofollow">Walid</a><br /><strong>Subject:</strong> 3758<br /><strong>Posted:</strong> 02-Nov-2012 at 2:36am<br /><br />it doesn't looks like Faking the ICompositionProvider is enough.<div><br></div><div>In the CreateEntityManagerCore you also access to&nbsp;EventFns which use the&nbsp;EventAggregatorLocator property. This one is coming from the PartLocator which use the composition to find the IEventAggregator. So if I fake the composition it can't it find the IEventAggregator.&nbsp;</div><div><br></div><div>Afterthought, making&nbsp;<span style=": rgb251, 251, 253; ">MefCompositionProvider</span><span style=": rgb251, 251, 253; ">&nbsp;public would be much simple.</span></div>]]>
   </description>
   <pubDate>Fri, 02 Nov 2012 02:36:59 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=3758&amp;PID=15054#15054</guid>
  </item> 
  <item>
   <title>Silverlight Unit testing and cocktail 2 beta : Well I don&amp;#039;t need IoC in...</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=3758&amp;PID=15052#15052</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=998" rel="nofollow">Walid</a><br /><strong>Subject:</strong> 3758<br /><strong>Posted:</strong> 01-Nov-2012 at 6:10pm<br /><br />Well I don't need IoC in my testing. The crash happen when&nbsp;accessing the property Composition.Provider&nbsp;in the method CreateEntityManagerCore (from the EntityManagerProvider).<div><br></div><div>I will check the ICompositionProvider<br><div><br><div><div><br></div><div><br></div></div></div></div>]]>
   </description>
   <pubDate>Thu, 01 Nov 2012 18:10:46 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=3758&amp;PID=15052#15052</guid>
  </item> 
  <item>
   <title>Silverlight Unit testing and cocktail 2 beta : In general, IoC shouldn&amp;#039;t...</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=3758&amp;PID=15051#15051</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=1005" rel="nofollow">mgood</a><br /><strong>Subject:</strong> 3758<br /><strong>Posted:</strong> 01-Nov-2012 at 6:02pm<br /><br />In general, IoC shouldn't be used for unit testing. You should fake composition by implementing a fake ICompositionProvider. I'm debating now making MefCompositionProvider public for cases where testing with MEF may make sense.]]>
   </description>
   <pubDate>Thu, 01 Nov 2012 18:02:55 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=3758&amp;PID=15051#15051</guid>
  </item> 
  <item>
   <title>Silverlight Unit testing and cocktail 2 beta : Hi,Before the migration to cocktail...</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=3758&amp;PID=15043#15043</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=998" rel="nofollow">Walid</a><br /><strong>Subject:</strong> 3758<br /><strong>Posted:</strong> 01-Nov-2012 at 1:53pm<br /><br />Hi,<div><br></div><div>Before the migration to cocktail 2 betal I had a working silverlight unit testing project. &nbsp;Since the migration it doesn't work anymore, it crash on when accessing the Composition.Provider property in the CreateEntityManager method.</div><div><br></div><div>Looking at the changes in cocktail it looks normal, &nbsp;having no bootstrapper in this project Mef isn't configured. I tried to manually create the MefCompositionProvider but it's not possible because it's an internal class. I checked the source of Cocktail I noticed this is exactly what you are doing in the Test Project but ... Cocktail makes its internal visible to it.</div><div><br></div><div>I don't know if it's possible to setup a boostrapper in this application because I don't have a ViewModel to give to it, the Viewmodel is created by the unit testing classes with the following method : UnitTestSystem.CreateTestPage()</div><div><br></div><div>How can I setup my silverlight unit testing application in order to have Mef configured ?</div>]]>
   </description>
   <pubDate>Thu, 01 Nov 2012 13:53:40 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=3758&amp;PID=15043#15043</guid>
  </item> 
 </channel>
</rss>