

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Plastic/Blog &#187; Mediator</title>
	<atom:link href="http://blog.codiceplastico.com/index.php/tag/mediator/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.codiceplastico.com</link>
	<description>Code Elegance</description>
	<lastBuildDate>Sat, 28 Aug 2010 10:27:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>How to mock the IMessageBroker</title>
		<link>http://blog.codiceplastico.com/index.php/2009/09/07/how-to-mock-the-imessagebroker/</link>
		<comments>http://blog.codiceplastico.com/index.php/2009/09/07/how-to-mock-the-imessagebroker/#comments</comments>
		<pubDate>Mon, 07 Sep 2009 07:36:07 +0000</pubDate>
		<dc:creator>Emanuele DelBono</dc:creator>
				<category><![CDATA[Emanuele DelBono]]></category>
		<category><![CDATA[Mediator]]></category>
		<category><![CDATA[MessageBroker]]></category>
		<category><![CDATA[Mock]]></category>
		<category><![CDATA[Moq]]></category>
		<category><![CDATA[MVVM]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://blog.codiceplastico.com/index.php/2009/09/07/how-to-mock-the-imessagebroker/</guid>
		<description><![CDATA[On the blogs there are a lot of articles about the M-V-VM pattern showing how to decouple the various components of our application to make it modular. One of the component that helps the ViewModels to communicate with other ViewModels is the MessageBroker (AKA EventAggregator, AKA Mediator): a manager class that route the messages from [...]]]></description>
			<content:encoded><![CDATA[<p>On the blogs there are a lot of articles about the M-V-VM pattern showing how to decouple the various components of our application to make it modular.</p>
<p>One of the component that helps the ViewModels to communicate with other ViewModels is the MessageBroker (AKA EventAggregator, AKA Mediator): a manager class that route the messages from the ViewModels.</p>
<p>One good implementation of the Mediator for the MVVM is this <a title="http://sachabarber.net/?p=477" href="http://sachabarber.net/?p=477">http://sachabarber.net/?p=477</a>:</p>
<p>To make it more testable we extract an interface:</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">interface</span> IMessageBroker
{
  <span class="kwrd">void</span> NotifyColleagues(MessageType messageType);
  <span class="kwrd">void</span> Register&lt;T&gt;(Action&lt;T&gt; action, MessageType messageType);
}</pre>
<p><!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } --></p>
<p> </p>
<p>If you TDD your application or if you write unit test you could need to mock the IMessageBroker and it’s not immediate because the Action&lt;T&gt;.</p>
<p><a href="http://blogs.ugidotnet.org/AMelchiori" target="_blank">Alessandro</a> wrote a solution that is interesting using <a href="http://code.google.com/p/moq/" target="_blank">Moq</a>:</p>
<pre class="csharpcode">[Fact]
<span class="kwrd">public</span> <span class="kwrd">void</span> Sample_Test()
{
  Mock&lt;IMessageBroker&gt; broker = <span class="kwrd">new</span> Mock&lt;IMessageBroker&gt;();
  Action&lt;String&gt; action = <span class="kwrd">null</span>;

  broker
    .Setup(b =&gt; b.Subscribe(It.IsAny&lt;Action&lt;String&gt;&gt;()))
    .Callback&lt;Action&lt;String&gt;&gt;(a =&gt; action = a);

  <span class="kwrd">new</span> FakeViewModel(broker.Object);

  action.Invoke(<span class="str">"TestMessage"</span>);

  <span class="rem">// Asserts...</span>
}</pre>
<p><!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } --></p>
<p>Moq is a powerful mock framework and it’s simplicity make the elegant and easy to read!</p>
<p> </p>
<div id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:4f4c6087-6f7c-4bce-a555-bbecf6e33351" class="wlWriterEditableSmartContent" style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px">Technorati Tags: <a rel="tag" href="http://technorati.com/tags/Moq">Moq</a>,<a rel="tag" href="http://technorati.com/tags/MVVM">MVVM</a>,<a rel="tag" href="http://technorati.com/tags/Mock">Mock</a>,<a rel="tag" href="http://technorati.com/tags/Test">Test</a>,<a rel="tag" href="http://technorati.com/tags/Mediator">Mediator</a>,<a rel="tag" href="http://technorati.com/tags/MessageBroker">MessageBroker</a></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.codiceplastico.com/index.php/2009/09/07/how-to-mock-the-imessagebroker/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
