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>) 调用失败,模拟行为严格。模拟上的所有调用都必须具有相应的设置。
有人可以帮我解决这个问题。任何帮助将不胜感激。谢谢