moq Mocking Behavior Mocking void methods to confirm what they return

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

var logRepository = new Mock<ILogRepository>();
logRepository.Setup(x => x.Write(It.IsAny<Exception>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
  .Verifiable();

In this case, we are using the Verifiable to ensure that it runs.

We could also use a callback here:

logRepository.Setup(x => x.Write(It.IsAny<Exception>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
     .Callback<Exception, string, string, string, string, string>((ex, caller, user, machine, source, message) => { Console.WriteLine(message); });

this would log the output from the method to standard output on the console (many testing frameworks let you capture that output into their runner)



Got any moq Question?