Mocks TraceListener with Moq

Why is the Moq Verify validation error "Moq.MockException: Invocation not executed on the layout"?

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)); 
+4
source share
2 answers

I know this question has been here for a long time, however the answer is as follows:

When you write a trace event through TraceSource, it calls the TraceEvent tracer to execute the trace - so you need to check the listener.TraceEvent calls, 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!

Greetings

Morgan

+4
source

The test indicates that you need to call the WriteLine method, but this is not the case. Looking at the code, this is most likely because this test detected an error in your TraceSource.TraceEvent or TraceSource.Flush . Double check these methods and you should be good to go!

Also see this question .

0
source

Source: https://habr.com/ru/post/1336612/


All Articles