How to mock Ilogger in nunit?

Raj
Raj
Member
496 Points
21 Posts

I'm using nunit project in .net 6.0 application. How to mock Ilogger in nunit?

Views: 440
Total Answered: 2
Total Marked As Answer: 1
Posted On: 20-Feb-2023 06:34

Share:   fb twitter linkedin
Answers
Priya
Priya
Participant
936 Points
28 Posts
         

Try as following:

public class ScheduleTest
{
    private Mock<IApplicationUnitOfWork> _applicationUnitOfWork;
    private Mock<ILogger<ScheduleService>> _logger;
    private IScheduleService _scheduleService;
    
    [SetUp]
    public void Setup()
    {
        _applicationUnitOfWork = new Mock<IApplicationUnitOfWork>();
        _logger = new Mock<ILogger<ScheduleService>>();
        _scheduleService = new ScheduleService(_applicationUnitOfWork.Object, _logger.Object);
    }

    [Test]
    [TestCase(0)]
    public async Task Schedule_Success(int testCase)
    {
        //
    }
}
Posted On: 20-Feb-2023 08:12
thanks.
 - Raj  12-Mar-2023 02:21
chatGPT
chatGPT
Member
92 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:

using Microsoft.Extensions.Logging;
using Moq;
using NUnit.Framework;

[TestFixture]
public class MyTests
{
    [Test]
    public void MyTest()
    {
        // create a mock ILogger<T> object
        var loggerMock = new Mock<ILogger<MyClass>>();

        // instantiate the class that uses the ILogger<T> object
        var myClass = new MyClass(loggerMock.Object);

        // call the method that writes to the log
        myClass.MyMethod();

        // verify that the log message was written
        loggerMock.Verify(
            x => x.Log(
                LogLevel.Information,
                It.IsAny<EventId>(),
                It.Is<It.IsAnyType>((o, t) => o.ToString().Contains("Log message")),
                null,
                It.IsAny<Func<It.IsAnyType, Exception, string>>()
            ),
            Times.Once
        );
    }
}

public class MyClass
{
    private readonly ILogger<MyClass> _logger;

    public MyClass(ILogger<MyClass> logger)
    {
        _logger = logger;
    }

    public void MyMethod()
    {
        _logger.LogInformation("Log message");
    }
}

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
 Log In to Chat