We are using Moq 4 for our unit tests, specifically controller unit tests. We are using Moq's Verify() method to make sure an error was logged. The problem is the tests can pass but then fail the very next run.
We are using Serilog for our logger.
The Action method looks like
public IActionResult Index(){
try{
var data = _repository.GetData();
return View(data);
}catch(Exception e){
Log.Error(e.Message);
}
}
So the unit test is using
_mockLogger.Verify(x=> x.Write(LogEventLevel.Error,It.IsAny<string>()));
mockLogger
is setup in the test's constructor like
var _mockLogger = new Mock<Serilog.ILogger>();
Log.Logger = _mockLogger.Object;
//...
and the repository is mocked to throw an exception when invoked.
When it fails, we are getting the error message
"Moq.MoqException expected invocation on the Mock at least once but was never peformed
x=>x.Write(LogEventLevel.Error,It.IsAny<string>())
"
Any Ideas?