在C#中,使用ILogger对静态方法进行单元测试的静态方法ILoggerFactory为空

问题描述

所有人

我正在尝试对代码进行单元测试,但是我无法使ILoggerFactory工作

这是我的单元测试代码(可能不正确):

using NUnit.Framework;
using Microsoft.Extensions.Logging;
using System.Reflection;
using Moq;
using System;
using MyProgramVIP.Bots.Presenter;
using MyProgramVIP.Bots.Model;
using MyProgramVIP.Bots.Utils;

namespace MyProgramVIPTest
{
    public class TestsExample
    {
        [SetUp]
        public void Setup() {
            
        }

        [Test]
        public void TestExample1()
        {
            //Mocks
            var mockLogger = new Mock<ILogger<TicketPresenter>>();
            mockLogger.Setup(
                m => m.Log(
                    LogLevel.@R_273_4045@ion,It.IsAny<EventId>(),It.IsAny<object>(),It.IsAny<Exception>(),It.IsAny<Func<object,Exception,string>>()));

            var mockLoggerFactory = new Mock<ILoggerFactory>();
            mockLoggerFactory.Setup(x => x.CreateLogger(It.IsAny<string>())).Returns(() => mockLogger.Object);

            //Construción del modelo necesario para la prueba
            ConversationData conversationData = new ConversationData();
            conversationData.ticket = new Ticket();
            conversationData.response = new Response();

            //Invocación del método a probar
            TicketPresenter.getPutTicketMessage(conversationData);

            //Comprobación del funcionamineto
            Assert.AreEqual("ticketType",conversationData.response.cardIdResponse);
        }
    }
}

这是我要测试的类的代码(我只剩下失败的代码行)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using MyProgramVIP.Bots.Interactor.Services;
using MyProgramVIP.Bots.Model;
using MyProgramVIP.Bots.Utils;

namespace MyProgramVIP.Bots.Presenter
{
    public class TicketPresenter
    {
        private static ILogger _logger = UtilsVIP.ApplicationLogging.CreateLogger(MethodBase.GetCurrentMethod().DeclaringType.Name);

        /// <summary>
        /// Función getPutTicketMessage: encargada de comenzar con el flujo de poner un ticket.
        /// </summary>
        /// <param name="conversationData"></param>
        public static void getPutTicketMessage(ConversationData conversationData)
        {
            try
            {
                //Here it crash.
                _logger.Log@R_273_4045@ion("INIT getPutTicketMessage");

                //Code..........

                //Never gets here.
                _logger.Log@R_273_4045@ion("ENDED getPutTicketMessage");
            }
            catch (Exception ex)
            {
                //Here it crash too.
                _logger.LogError("EXCEPTION getPutTicketMessage" + ex.ToString());
            }
        }
}

这是错误所在的帮助类的代码

using System;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using MyProgramVIP.Bots.Model;
using MyProgramVIP.Bots.Model.Elements;

namespace MyProgramVIP.Bots.Utils
{
    public class UtilsVIP
    {
        private static ILogger _logger = ApplicationLogging.CreateLogger("ILogger");

        //Other funtions...

        public static class ApplicationLogging
        {
            public static ILoggerFactory LoggerFactory { get; set; }
            public static ILogger CreateLogger<T>() => LoggerFactory.CreateLogger<T>();
            //Here LoggerFactory is null.
            public static ILogger CreateLogger(string categoryName) => LoggerFactory.CreateLogger("VIPLog: " + categoryName);

        }

        //Other funtions...
    }
}

就在它运行的行上:

LoggerFactory.CreateLogger("VIPLog: " + categoryName);

LoggerFactory为空。

我已经在互联网上做了大量研究,但是所有这些都会导致非静态的班级信息。

如果重要的话,这是Botframework项目。

任何帮助将不胜感激。

谢谢。

解决方法

非常感谢@ChetanRanpariya的评论,我的问题的答案非常简单:

keywords
hummm asd yes
blow com nope
rest hiya mouse
wow good com

只需添加以下内容:

using NUnit.Framework;
using Microsoft.Extensions.Logging;
using System.Reflection;
using Moq;
using System;
using MyProgramVIP.Bots.Presenter;
using MyProgramVIP.Bots.Model;
using MyProgramVIP.Bots.Utils;

namespace MyProgramVIPTest
{
    public class TestsExample
    {
        [SetUp]
        public void Setup() {
            
        }

        [Test]
        public void TestExample1()
        {
            //Mocks
            var mockLogger = new Mock<ILogger<TicketPresenter>>();
            mockLogger.Setup(
                m => m.Log(
                    LogLevel.Information,It.IsAny<EventId>(),It.IsAny<object>(),It.IsAny<Exception>(),It.IsAny<Func<object,Exception,string>>()));

            var mockLoggerFactory = new Mock<ILoggerFactory>();
            mockLoggerFactory.Setup(x => x.CreateLogger(It.IsAny<string>())).Returns(() => mockLogger.Object);

            //Just add this,I guess is replacing the objet with the mock.
            UtilsVIP.ApplicationLogging.LoggerFactory = mockLoggerFactory.Object;
            
            //Construción del modelo necesario para la prueba
            ConversationData conversationData = new ConversationData();
            conversationData.ticket = new Ticket();
            conversationData.response = new Response();

            //Invocación del método a probar
            TicketPresenter.getPutTicketMessage(conversationData);

            //Comprobación del funcionamineto
            Assert.AreEqual("ticketType",conversationData.response.cardIdResponse);
        }
    }
}

感谢您的帮助。

,

在此示例如何验证使用Moq在ILogger上调用了日志。

 _loggerMock.Verify
        (
            l => l.Log
            (
                //Check the severity level
                LogLevel.Error,//This may or may not be relevant to your scenario
                It.IsAny<EventId>(),//This is the magical Moq code that exposes internal log processing from the extension methods
                It.Is<It.IsAnyType>((state,t) =>
                    //This confirms that the correct log message was sent to the logger. {OriginalFormat} should match the value passed to the logger
                    //Note: messages should be retrieved from a service that will probably store the strings in a resource file
                    CheckValue(state,LogTest.ErrorMessage,"{OriginalFormat}") &&
                    //This confirms that an argument with a key of "recordId" was sent with the correct value
                    //In Application Insights,this will turn up in Custom Dimensions
                    CheckValue(state,recordId,nameof(recordId))
            ),//Confirm the exception type
            It.IsAny<NotImplementedException>(),//Accept any valid Func here. The Func is specified by the extension methods
            (Func<It.IsAnyType,string>)It.IsAny<object>()),//Make sure the message was logged the correct number of times
            Times.Exactly(1)
        );

    private static bool CheckValue(object state,object expectedValue,string key)
    {
        var keyValuePairList = (IReadOnlyList<KeyValuePair<string,object>>)state;

        var actualValue = keyValuePairList.First(kvp => string.Compare(kvp.Key,key,StringComparison.Ordinal) == 0).Value;

        return expectedValue.Equals(actualValue);
    }

this article中有更多上下文。