Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You didn't post your tests, so I'm unclear what you are trying to test, but most-likely you are trying to test one of the following things:</p> <ol> <li>That the code you are testing subscribes at all</li> <li>That the code you are testing reacts to events appropriately</li> </ol> <p>In either case, you are going to want to <em>mock the EventAggregator</em>. Because the Event isn't what you want to test, but rather the code that utilizes it, you want to provide a fake alternative that does what you want it to do. I'll try and provide a good example. I use Moq, but you can choose whatever mocking framework you like.</p> <p>In this test, I simply assert that Subscribe was called in the constructor, but your test might be more complicated if you are wanting to test the class's reaction to a raised event. The test shows a <code>CompositePresentationEvent&lt;int&gt;</code>.</p> <pre><code>//Arrange Mock&lt;MyEvent&gt; mockEvent = new Mock&lt;MyEvent&gt;(); Mock&lt;IEventAggregator&gt; mockAggregator = new Mock&lt;IEventAggregator&gt;(); mockEvent.Setup ( evnt =&gt; evnt.Subscribe(It.IsAny&lt;Action&lt;int&gt;&gt;()) ); mockAggregator.Setup ( agg =&gt; agg.GetEvent&lt;MyEvent&gt;() .Returns(mockEvent.Object); ); //Act MyClassIWantToTest target = new MyClassIWantToTest(mockAggregator.Object); //Assert mockEvent.VerifyAll(); </code></pre> <p>That's the basics. The rule of thumb here is that if you tests rely on a system resource that is difficult to provide, isolate it from the test.</p> <p><strong>Edit: after reading your question I see you are trying to test the callback.</strong> </p> <p>In this sample, I test if the "CurrentValueProperty" property is set to whatever value is passed in the callback method. Here is that sample:</p> <pre><code>//Arrange Mock&lt;MyEvent&gt; mockEvent = new Mock&lt;MyEvent&gt;(); Mock&lt;IEventAggregator&gt; mockAggregator = new Mock&lt;IEventAggregator&gt;(); Action&lt;int&gt; theEventCallback = null; mockEvent.Setup ( evnt =&gt; evnt.Subscribe(It.IsAny&lt;Action&lt;int&gt;&gt;()) ) .Callback&lt;Action&lt;int&gt;&gt; ( cb =&gt; theEventCallback = cb ); mockAggregator.Setup ( agg =&gt; agg.GetEvent&lt;MyEvent&gt;() ) .Returns(mockEvent.Object); //Act MyClassIWantToTest target = new MyClassIWantToTest(mockAggregator.Object); //we expect this to be populated by the callback specified in our mock setup //that will be triggered when Subscribe is called in //MyClassIWantToTest's constructor theEventCallback(27); //Assert Assert.AreEqual(target.CurrentValueProperty, 27); </code></pre> <p>That's it.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload