1

I have a linked question here. I read this and I'm trying to mock the dependencies in my Hosted Service. But I'm confused as to assert or verify what. I'm thinking to verify that StartConsumption on Mock Kafka Topic Consumer Manager gets invoked. But to set it up, it returns void. I'm not able to determine how to set up the predicate for the mock.

services.AddSingleton(Mock.Of<IKafkaTopicConsumerManager>(_ => 
    _.StartConsumption(
       It.IsAny<CancellationToken>(),
       It.IsAny<IMessageProcessingCapable>(),
       It.IsAny<string>(),
       It.IsAny<string>(),
       It.IsAny<List<string>>(),
       It.IsAny<string>(),
       It.IsAny<int>())==void
    ));

But at void it shows up invalid expression term. Below is my Worker.cs which is the Hosted Service.

protected override async Task ExecuteAsync(CancellationToken cancellationToken)
{
    _consumerManager.StartConsumption(
            cancellationToken,
            _messageProcessor,
            _config.GetSection("Kafka:Servers").Get<string>(),
            _config.GetSection("ConsumerGroup").Get<string>(),
            _config.GetSection("Topics").Get<List<string>>(),
            _config.GetSection("Kafka:TopicSuffix").Get<string>(),
            _config.GetSection("ConsumerThreads").Get<int>());
}
Nkosi
  • 235,767
  • 35
  • 427
  • 472
Raida Adn
  • 405
  • 1
  • 6
  • 17

1 Answers1

0

Just add the mock. Since the invoked member is void, then there is nothing to return. Thus no need to set it up to do anything.

services.AddSingleton(Mock.Of<IKafkaTopicConsumerManage>());

What you can do, is verify that the mocked member was invoked after exercising the subject under test

var _mockedKafkaTopicConsumerManager = Mock.Get(serviceProvider.GetService<IKafkaTopicConsumerManage>());
_mockedKafkaTopicConsumerManager
    .Verify(c => c.StartConsumption(
        It.IsAny<CancellationToken>(), 
        _mockedMessageProcessor.Object,
        It.IsAny<string>(),
        It.IsAny<string>(),
        It.IsAny<List<string>>(),
        It.IsAny<string>(),
        It.IsAny<int>()
),Times.Once);
Nkosi
  • 235,767
  • 35
  • 427
  • 472