Mocking a CAL EventAggregator with Moq

I am using the Composite Application Library event aggregator and would like to create a layout for IEventAggregator for use in my unit test.

I plan to use Moq for this task, and an example test 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 to know if there is some smart way to avoid defining an empty layout for each type of event that my code might encounter (SomeOtherEvent, SomeThirdEvent, ...)? I could, of course, define all my events this way in the [TestInitialize] method, but I would like to know if there is a smarter way ?:-)

+3
1

:

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

mockEventAggregator mocks .

+1

Source: https://habr.com/ru/post/1704655/


All Articles