c# - 无法在 Nunit 中模拟 ILogger
问题描述
Net core 和 NUnit 测试用例。我的控制器如下。
public class MyController : ControllerBase
{
public MyController(ILogger<MyController > logger)
{
this.Logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
private readonly ILogger<MyController > Logger;
public async Task<ActionResult> GetList()
{
this.Logger.LogInformation($"MyList: Some results came");
}
}
然后在我的单元测试用例下方。
public class MyControllerTests
{
private Mock<ILogger<MyController>> Logger = new Mock<ILogger<MyController>>();
internal MyController myController;
public MyControllerTests()
{
this.myController= new MyController(this.Logger.Object);
}
[Test]
public async Task ListTest()
{
Logger.Verify(
x => x.Log(
LogLevel.Information,
It.IsAny<EventId>(),
It.Is<It.IsAnyType>((o, t) => string.Equals("MyList: Some results came", o.ToString(), StringComparison.InvariantCultureIgnoreCase)),
It.IsAny<Exception>(),
(Func<It.IsAnyType, Exception, string>)It.IsAny<object>()),
//rest of the code but i am getting error in above line
}
}
上面的代码给出了错误
Moq.MockException : ILogger.Log(LogLevel.Information, 0, ListTest : 成功获取结果, null, Func<FormattedLogValues, Exception, string>) 调用失败,模拟行为严格。模拟上的所有调用都必须具有相应的设置。
有人可以帮我解决这个问题。任何帮助将不胜感激。谢谢
解决方案
你很亲密。这是一篇关于此的文章。这是我提出的两个工作验证:
_logTest.Process();
_loggerMock.Verify(l => l.Log(
LogLevel.Information,
It.IsAny<EventId>(),
It.IsAny<It.IsAnyType>(),
It.IsAny<Exception>(),
(Func<It.IsAnyType, Exception, string>)It.IsAny<object>()), Times.Exactly(1));
此版本允许您获得更具体的信息:
_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, "MyList: Some results came", "{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<ArgumentNullException>(),
//Accept any valid Func here. The Func is specified by the extension methods
(Func<It.IsAnyType, Exception, string>)It.IsAny<object>()),
//Make sure the message was logged the correct number of times
Times.Exactly(1)
);
推荐阅读
- php - 创建 JSON 对象并使用 PHP CURL 访问它在解码时返回空白
- node.js - Mongodb、Reactjs、Express、Node js Heroku 部署显示空白页和 404 错误。在本地工作
- javascript - 在 React 测试库中的 Ant Design Slider 上触发 onChange 函数不起作用
- java - 从 Java 中的控制台读取时如何设置默认输入?
- java - 如何在 Java Springboot 中使用 Jackson 在自动启动时加载 JSON 配置文件
- javascript - 元素相互叠加,直到我第二次加载页面
- java - 在休眠期间清理连接池需要太长时间
- mysql - mysql select query WHERE json_contains 匹配但不返回数据?
- python - “无”出现在输出中,我不知道为什么
- asp.net - nopCommerce 4.2:如何在 nopCommerce 上配置 SSL 相关设置以在商店主页上使用 https