作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在使用 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)
注:FormattedLogValues
是 internal struct
所以我真的不得不使用 Arg.Any<object>
.
当我检查实际收到的调用时,我看到记录器有一个调用,但我不太确定它与我所断言的有何不同。就像我在 var re = logger.ReceivedCalls();
这样的东西上放置了一个断点在调试我的单元测试时:
我真的不知道什么不被视为 NSubstitute 的正确参数 Received()
断言。
有什么想法吗?
最佳答案
如果参数匹配非常困难,我会使用 ReceivedWithAnyArgs()
( https://nsubstitute.github.io/help/received-calls/ )。如果事情很简单,NSubstitute 就很好。如果它变得太复杂,我倾向于只创建接口(interface)的测试版本(例如 ILogger),并从那里的调用中注册我感兴趣的参数,并检查这些参数。
关于c# - 如何正确检查 ILogger.LogCritical 是否已使用 NSubstitute 调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59292245/
我在使用 NSubstitute 检查方法 ILogger.LogCritical(...) 时遇到了一些困难已被调用。 例如下面的代码: [Fact] public void TestNSubsti
我是一名优秀的程序员,十分优秀!