0

I am writing Unit Test using MOCK (Moq) for my Azure Event Hub. The Unit Test is simple. When I send 1 message to my Event Hub, I need to make sure it does receive 1 message. This is what I am doing...

Mock<IEventHub> eventHubMock;

eventHubMock
    .Setup( client => client.SendBatchAsync( It.IsAny<IEnumerable<EventData>>() ) )
    .Returns( Task.CompletedTask );

// Some processing which sends 1 message to Event Hub.

// Now to verify that my mock did receive 1 message.

eventHubMock.Verify(
    client => client.SendBatchAsync(
    It.IsAny<IEnumerable<EventData>>() ) );

QUESTIONS

  1. The above will just validate it received some EventData. How do I verify that it received 1 EventData ?
  2. What other validation can I perform using Mock?
  3. My code deals with 2 event Hubs. How do I make sure the message is sent to correct event hub ?
3
  • To verify that you pass only 1 EventData you can setup it like this: It.Is<IEnumerable<EventData>>(e => e.ToList().Count == 1).The rule is 1 question per question(?). Also, your second question doesn't make sense...
    – FCin
    Feb 24, 2018 at 7:33
  • The question in its current state is incomplete and therefore unclear. Read How to Ask and then edit the question to provide a minimal reproducible example that can be used to reproduce the problem, allowing a better understanding of what is being asked.
    – Nkosi
    Feb 24, 2018 at 12:33
  • There is an extra parameter you can give to Verify to say how many times you expect it to be called. Try eventHubMock.Verify( client => client.SendBatch( your parameters here ), Times.Once); Mar 23, 2018 at 23:05

1 Answer 1

1

You should use It.Is<T>(Expression) method to do verifications of parameters:

eventHubMock.Verify(
    client => client.SendBatch(
        It.Is<IEnumerable<EventData>>(es => es.Count() == 0 && IsValid(es.First()))));

where IsValid(EventData eventData) is some helper method to check all that you want about the event.

Which Event Hub the event goes to is defined when you create the sender, not when you use it. So the check should be related to something that happens inside Some processing which sends 1 message to Event Hub block, hard to give any details right now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.