views:

404

answers:

2

Hi,

I'm using the Composite Application Library's event aggregator, and would like to create a mock for the IEventAggregator interface, for use in my unit test.

I'm planning on using Moq for this task, and an example test so far looks something like this:

var mockEventAggregator = new Mock<IEventAggregator>();
var mockImportantEvent = new Mock<ImportantEvent>();
mockEventAggregator.Setup(e => e.GetEvent<SomeOtherEvent>()).Returns(new Mock<SomeOtherEvent>().Object);
mockEventAggregator.Setup(e => e.GetEvent<SomeThirdEvent>()).Returns(new Mock<SomeThirdEvent>().Object);
// ...
mockEventAggregator.Setup(e => e.GetEvent<ImportantEvent>()).Returns(mockImportantEvent.Object);

mockImportantEvent.Setup(e => e.Publish(It.IsAny<ImportantEventArgs>()));

// ...Actual test...

mockImportantEvent.VerifyAll();

This works fine, but I would like know, if there is some clever way to avoid having to define an empty mock for every event-type my code might encounter (SomeOtherEvent, SomeThirdEvent, ...)? I could of course define all my events this way in a [TestInitialize] method, but I would like to know if there is a more clever way? :-)

A: 

I found the solution for this one:

var mockEventAggregator = new Mock<IEventAggregator>{ DefaultValue = DefaultValue.Mock };

will make the mockEventAggregator return mocks for all nested objects.

toxvaerd
A: 

I am facing the following issue and appreciate your help for I have run out of ideas as how to fix.

I have the following in my test: Mock> mockToolbarClickEvent3 = new Mock>(); _aggregator.Setup(e => e.GetEvent>()).Returns(mockToolbarClickEvent3.Object);

In my code (that I am testing) I have the following for Publishing an event:

SomeObject someoject = new SomeObject(); _aggregator.GetEvent>().Publish(someObject);

where SomeObject has a Filter method that returns true or false.

And for subscribing I have the following: mc.Subscribe(myCallback, ThreadOption.PublisherThread, true, target => target.Filter());

However when I make the call to Publish nothing happens and the event is not received by the subscribing methods and modules.

What am I missing?

Thank you so much in advance,

ajdotnet