Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Mocking Event handling will not cover delegate function assigned

Trying to mock an EventHandler for an Event raised from another class. However, when I run the test, the test does not cover the code that implemented inside the handler.

Test

[Fact]
public void ShouldConsumeFromTopicAndRaiseEvent()
{
    ConsumerEventMessage message = new ConsumerEventMessage()
    {
        Message = "Test"
    };
    
    _consumerHandlerMock.Setup(_ =>
        _.Consume(It.IsAny<string>(),
            It.IsAny<CancellationToken>()));
    _consumerHandlerMock.Raise(_ => 
        _.HandleConsumeMessageEvent += null, message);
    _consumerHandlerMock.Object.HandleConsumeMessageEvent += (sender, eventMessage) =>
    {
        FetchNotificationDataByApplicationIdMessage notificationDataByApplicationIdMessage =
            new FetchNotificationDataByApplicationIdMessage()
            {
                ApplicationId = eventMessage.Message
            };
        _fetchNotificationDataByApplicationIdEventMock.Raise(_ =>
            _.FetchNotificationDataByApplicationIdEvent += null, notificationDataByApplicationIdMessage);
    };
    
   Task result = _fetchNotificationDataByApplicationIdConsumer
        .StartAsync(It.IsAny<CancellationToken>());
   result.Wait();
   result.Should().Be(Task.CompletedTask);
}

Implementation

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

private void SubscribeToEvent(CancellationToken cancellationToken)
{
    _consumerHandler.HandleConsumeMessageEvent += (sender, message) =>
    {
        FetchNotificationDataByApplicationIdMessage fetchNotificationDataByApplicationId = 
            new FetchNotificationDataByApplicationIdMessage()
        {
            ApplicationId = message.Message
        };
        FetchNotificationDataByApplicationIdEvent(this, fetchNotificationDataByApplicationId);
    };
    _consumerHandler.Consume(_topic, cancellationToken);
}

>Solution :

The setup was done incorrectly.

Reference: MOQ Quickstart: Events

[Fact]
public async Task ShouldConsumeFromTopicAndRaiseEvent() {
    // Arrange
    ConsumerEventMessage message = new ConsumerEventMessage() {
        Message = "Test"
    };
    
    // Causes an event to raise automatically when Consume is invoked
    _consumerHandlerMock
        .Setup(_ => _.Consume(It.IsAny<string>(), It.IsAny<CancellationToken>()))
        .Raises(_ => _.HandleConsumeMessageEvent += null, message);

    // Act
    await _fetchNotificationDataByApplicationIdConsumer.StartAsync(CancellationToken.None);
   
    // Assert....
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading