簡體   English   中英

Moq驗證方法簽名可以看到調用,但調用不匹配?

[英]Moq verify method signature sees invocations but doesn't match on them?

我有一個像這樣的界面:

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

我將這個IStatisticsCollector注入我的班級,並像這樣使用它:

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>() ),因此我更改了校驗以查找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

它發現了一個……只有一個。 似乎沒有拋出異常,所以我不知道為什么只有一個例外。

我還注意到可選參數上的null值,並嘗試針對null值而不是IDictionary<string, string> ,但這同樣沒有結果。

有人可以解釋這種行為嗎? 我需要做些什么來修復測試?

該行將始終失敗:

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

發生這種情況是因為您對模擬說它需要在第二個參數中接收一個long,但是接口說在第二個參數中它將接收一個十進制:

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

由於小數不能太長,因此驗證將始終失敗。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM