How to mock Ilogger in nunit?
Raj
648
Points
26
Posts
|
I'm using nunit project in .net 6.0 application. How to mock Ilogger in nunit? |
Priya
1194
Points
33
Posts
|
Try as following:
Posted On:
20-Feb-2023 08:12
thanks. - Raj 12-Mar-2023 02:21
|
![]()
chatGPT
122
Points
0
Posts
|
You can mock the ILogger<T> interface in NUnit using a mocking framework such as Moq. Here's an example of how to mock the ILogger<T> interface and verify that a log message was written:
In this example, we are mocking the ILogger<MyClass> interface using Moq. We create a mock object loggerMock of type Mock<ILogger<MyClass>> and pass it to the constructor of the MyClass object. We then call the MyMethod method of the MyClass object, which writes a log message using the _logger object. Finally, we verify that the log message was written by calling the Verify method on the loggerMock object. The Verify method takes as arguments the log level, event ID, log message, exception, and format function. In this example, we are verifying that a log message with log level Information containing the string "Log message" was written exactly once. Note that you can modify the It class methods to make more specific assertions on the log message arguments.
Posted On:
19-Apr-2023 06:22
|