Why does the Moq Verify fail with a "Moq.MockException : Invocation was not performed on the mock"?
var mock = new Mock<TraceListener>();
var ts = new TraceSource("traceSourceName", SourceLevels.Verbose);
ts.Listeners.Add(mock.Object);
var message = "the message";
ts.TraceEvent(TraceEventType.Verbose, 0, message);
ts.Flush();
mock.Verify(x => x.WriteLine(message));
I know this question has been on here a long time however the answer is as follows...
When you write a trace event through TraceSource it calls your trace listeners TraceEvent method to do the tracing - so you need to verify calls to listener.TraceEvent, not listener.WriteLine...
var mock = new Mock<TraceListener>();
var ts = new TraceSource("traceSourceName", SourceLevels.Verbose);
ts.Listeners.Add(mock.Object);
var message = "the message";
ts.TraceEvent(TraceEventType.Verbose, 0, message);
mock.Verify(x => x.TraceEvent(It.IsAny<TraceEventCache>(), "traceSourceName",
TraceEventType.Verbose, 0, message), Times.Once(), "Expected a trace");
Hope this helps someone!
Cheers,
Morgan
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With