gpt4 book ai didi

c# - 如何正确检查 ILogger.LogCritical 是否已使用 NSubstitute 调用?

转载 作者:行者123 更新时间:2023-12-03 08:50:29 29 4
gpt4 key购买 nike

我在使用 NSubstitute 检查方法 ILogger.LogCritical(...) 时遇到了一些困难已被调用。

例如下面的代码:

[Fact]
public void TestNSubstituteAgain()
{
var logger = Substitute.For<ILogger<StockService>>();
logger.LogCritical(new Exception(), "Hey lads!");
logger.Received().Log(
LogLevel.Critical,
Arg.Any<EventId>(),
Arg.Any<object>(),
Arg.Any<Exception>(),
Arg.Any<Func<object, Exception, string>>()
);
}

我遇到了这个异常:

Rm.Combo.App.Tests.VirtualParcels.Services.StockServiceTest.TestNSubstituteAgain

NSubstitute.Exceptions.ReceivedCallsException : Expected to receive a call matching:
Log<Object>(Critical, any EventId, any Object, any Exception, any Func<Object, Exception, String>)
Actually received no matching calls.

at NSubstitute.Core.ReceivedCallsExceptionThrower.Throw(ICallSpecification callSpecification, IEnumerable`1 matchingCalls, IEnumerable`1 nonMatchingCalls, Quantity requiredQuantity)
at NSubstitute.Routing.Handlers.CheckReceivedCallsHandler.Handle(ICall call)
at NSubstitute.Routing.Route.Handle(ICall call)
at NSubstitute.Core.CallRouter.Route(ICall call)
at NSubstitute.Proxies.CastleDynamicProxy.CastleForwardingInterceptor.Intercept(IInvocation invocation)
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at NSubstitute.Proxies.CastleDynamicProxy.ProxyIdInterceptor.Intercept(IInvocation invocation)
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Castle.Proxies.ObjectProxy_2.Log[TState](LogLevel logLevel, EventId eventId, TState state, Exception exception, Func`3 formatter)
at Rm.Combo.App.Tests.VirtualParcels.Services.StockServiceTest.TestNSubstituteAgain() in C:\Users\eperret\Desktop\combo\api\Rm.Combo.App.Tests\VirtualParcels\Services\StockServiceTest.cs:line 99
// 1] Ok the call I am making is that ext. method below:
public static void LogCritical(this ILogger logger, Exception exception, string message, params object[] args)
{
logger.Log(LogLevel.Critical, exception, message, args);
}

// 2] Which forwards to that other ext. method below:
public static void Log(this ILogger logger, LogLevel logLevel, Exception exception, string message, params object[] args)
{
logger.Log(logLevel, 0, exception, message, args);
}

// 3] and... then here (still an ext. method tho):
public static void Log(this ILogger logger, LogLevel logLevel, EventId eventId, Exception exception, string message, params object[] args)
{
if (logger == null)
{
throw new ArgumentNullException(nameof(logger));
}

logger.Log(logLevel, eventId, new FormattedLogValues(message, args), exception, _messageFormatter);
}

// 4] to finally end up with this "direct" interface method:
void Log<TState>(
LogLevel logLevel,
EventId eventId,
TState state,
Exception exception,
Func<TState, Exception, string> formatter);
// Note: which is supposedly what I am gonna ask NSubstitute to check against, right? (since we cannot use NSubstitute `Received` method on ext. method, it has to be the corresponding instance method)

注:FormattedLogValuesinternal struct所以我真的不得不使用 Arg.Any<object> .

当我检查实际收到的调用时,我看到记录器有一个调用,但我不太确定它与我所断言的有何不同。就像我在 var re = logger.ReceivedCalls(); 这样的东西上放置了一个断点在调试我的单元测试时:

enter image description here

我真的不知道什么不被视为 NSubstitute 的正确参数 Received()断言。

有什么想法吗?

最佳答案

如果参数匹配非常困难,我会使用 ReceivedWithAnyArgs() ( https://nsubstitute.github.io/help/received-calls/ )。如果事情很简单,NSubstitute 就很好。如果它变得太复杂,我倾向于只创建接口(interface)的测试版本(例如 ILogger),并从那里的调用中注册我感兴趣的参数,并检查这些参数。

关于c# - 如何正确检查 ILogger.LogCritical 是否已使用 NSubstitute 调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59292245/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com