title: Unit testing with Moq, Prism 6, and Event Aggregation
body: <p>I want to unit test a module by throwing messages at it via Event Aggregation to make sure it responds appropriately, either by setting properties appropriately, or by publishing other messages as a result. I am using Prism 6.
  In my project, the infrastructure project has:</p>
  
  <pre><code> public class ImportantMessage : PubSubEvent&lt;string&gt;
   {
   }
  </code></pre>
  
  <p>ModuleA publishes a message like this:</p>
  
  <pre><code>eventAggregator.GetEvent&lt;ImportantMessage&gt;().Publish(importantString);
  </code></pre>
  
  <p>ModuleB receives the message like this:</p>
  
  <pre><code>eventAggregator.GetEvent&lt;ImportantMessage&gt;().Subscribe(HandleImportantMessage);
  </code></pre>
  
  <p>Here is HandleImportantMessage:</p>
  
  <pre><code>public void HandleImportantMessage(string importantString)
  {
     . . .
  }
  </code></pre>
  
  <p>The ModuleB constructor is called as follows:</p>
  
  <pre><code>ModuleB(IEventAggregator EventAggregator)
  </code></pre>
  
  <p>This constructor is called by the Prism framework. For unit testing, I need to create an instance of ModuleB, and pass an IEventAggregator, probably a fake one created by Moq. And I want to do this in such a way that the message I publish carries importantString with it.
  If I Google the phrase “unit tests with moq and event aggregation,” there are
  several references, but I didn’t see how to use any of these approaches to pass “importantString” from ModuleA To ModuleB. The sample code for Prism 5 creates a fake event aggregator, but without using Moq. I don't understand how it works, and don't see how to pass a string with it.</p>
  
  <p>My test code starts off something like this:</p>
  
  <pre><code>var moqEventAggregator = new Mock(IEventAggregator);
  var moqImportantMessage = new Mock&lt;ImportantMessage&gt;();
  moqEventAggregator.Setup(x =&gt; x.GetEvent&lt;ImportantMessage&gt;());
  </code></pre>
  
  <p>Some of the references I have seen apply something like .Returns(eventBeingListenedTo.Object); 
  to moqEventAggregator after Setup is applied.
  I obviously need to apply .Setup(something) to moqImportantMessage in order to pass importantString, but I don't see exactly what yet.</p>
  
  <p>What am I missing? How do I pass a string with the fake published message?</p>