Moq – Verifying parameter values on a mocked method call.

moq logo

Was going down a rabbit hole trying to figure out how to check param value on a mocked method (pretty simple if truth be told).

I need to ensure that when an exception was raised my logger would be called Times.Once and would log the correct level of detail in the message.  I did the following:

  • Unit test to mock method under test to raise an exception;
  • Mocked setup on my logger;
  • Assert with Verify on my mocked logger

_mockLogger.Verify(m => m.LogException(It.IsAny<Exception>(), It.Is<String>(l => l == “Message containing detail that I care about to be logged”));

Work related code has been obfuscated for the purposes of this blog. 

Please check out some of my other Moq articles including mocking a return type of task and tuple

Kudus to the following on StackOverflow which reminded me of “It.Is” here

Leave a Reply