gpt4 book ai didi

c# - Moq 验证方法签名看到调用但不匹配?

转载 作者:行者123 更新时间:2023-11-30 21:30:35 26 4
gpt4 key购买 nike

我有这样一个界面:

public interface IStatisticsCollector : IDisposable
{
Task Measure(string metricName, decimal value, IDictionary<string, string> tags = null);
}

我正在注入(inject)这个 IStatisticsCollector进入我的类(class)并像这样使用它:

var stopwatch = new Stopwatch();
await dataCollector.Measure("rbk_init", stopwatch.ElapsedMilliseconds);
...
await dataCollector.Measure("rbk_compiled", stopwatch.ElapsedMilliseconds);
...

设置我的单元测试以验证我是否记录了我想要的所有统计点,我模拟了 IStatisticsCollector :

private readonly Mock<IStatisticsCollector> _statisticsCollector = new Mock<IStatisticsCollector>();
_statisticsCollector.Setup(x => x.Measure(It.IsAny<string>(), It.IsAny<long>(), It.IsAny<IDictionary<string, string>>())).Verifiable();

当我运行我的单元测试时,我在这一行验证失败:

//assert
_statisticsCollector.Verify(
x => x.Measure(It.IsAny<string>(), It.IsAny<long>(), It.IsAny<IDictionary<string, string>>()), Times.Exactly(5));

...带有以下消息:

Moq.MockException : 
Expected invocation on the mock exactly 5 times, but was 0 times: x => x.Measure(It.IsAny<string>(), (decimal)It.IsAny<long>(), It.IsAny<IDictionary<string, string>>())

Configured setups:
IStatisticsCollector x => x.Measure(It.IsAny<string>(), (decimal)It.IsAny<long>(), It.IsAny<IDictionary<string, string>>())

Performed invocations:
IStatisticsCollector.Measure("rbk_init", 31, null)
IStatisticsCollector.Measure("rbk_compiled", 35, null)
IStatisticsCollector.Measure("rbk_stored", 36, null)
IStatisticsCollector.Measure("rbk_db_updated", 352, null)
IStatisticsCollector.Measure("rbk_completed", 361, null)
at Moq.Mock.VerifyCalls(Mock targetMock, InvocationShape expectation, LambdaExpression expression, Times times, String failMessage) in C:\projects\moq4\src\Moq\Mock.cs:line 378

...这很奇怪,因为看起来它捕获了 5 个匹配的调用,但显然没有将它们中的任何一个视为实际匹配。现在我假设这可能与秒表的 ElapsedMilliseconds 的事实有关。很长,但界面需要小数(隐式转换),所以我更改了验证以查找 It.IsAny<decimal>() ,但这给了我一个意想不到的结果:

Moq.MockException : 
Expected invocation on the mock exactly 5 times, but was 1 times: x => x.Measure(It.IsAny<string>(), It.IsAny<decimal>(), It.IsAny<IDictionary<string, string>>())

Configured setups:
IStatisticsCollector x => x.Measure(It.IsAny<string>(), It.IsAny<decimal>(), It.IsAny<IDictionary<string, string>>())

Performed invocations:
IStatisticsCollector.Measure("rbk_init", 28, null)
at Moq.Mock.VerifyCalls(Mock targetMock, InvocationShape expectation, LambdaExpression expression, Times times, String failMessage) in C:\projects\moq4\src\Moq\Mock.cs:line 378

它找到了一个……而且只有一个。似乎没有抛出异常,所以我不知道为什么只有一个。

我还注意到可选参数上的空值,并尝试根据空值而不是 IDictionary<string, string> 进行验证, 但这同样没有结果。

有人可以解释这种行为吗?我需要做什么来修复我的测试?

最佳答案

这一行总是会失败:

_statisticsCollector.Verify(
x => x.Measure(It.IsAny<string>(), It.IsAny<long>(), It.IsAny<IDictionary<string, string>>()), Times.Exactly(5));

发生这种情况是因为您对模拟说它需要在第二个参数中接收一个 long,但是接口(interface)说在第二个参数中它将接收一个小数:

public interface IStatisticsCollector : IDisposable
{
Task Measure(string metricName, decimal value, IDictionary<string, string> tags = null);
}

由于小数点不能太长,您的验证将始终失败。

关于c# - Moq 验证方法签名看到调用但不匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54150344/

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