【发布时间】:2020-09-18 17:26:28
【问题描述】:
无论我如何进行常规设置,尝试对 Mocked 依赖项调用 verify 总是失败。
这是我希望模拟的接口上的方法签名:
Task EnqueueAsync<T>(string factoryId, string clientId, T messageObject, string messageId = null, string messageLabel = null, bool forcePersistence = true, DateTime? scheduledEnqueueTimeUtc = null, Dictionary<string, object> properties = null, double retryTime = 0);
这是我的设置方法:
mockTopic = new Mock<ITopic>();
mockTopic.Setup(m => m.EnqueueAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<DateTime?>(), It.IsAny<Dictionary<string, object>>(), It.IsAny<double>()))
.Callback((string factoryId, string clientId, string messageObject, string messageId, string messageLabel, bool forcePersistence, DateTime? scheduledEnqueueTimeUtc, Dictionary<string, object> properties, double retryTime) =>
{
testProperties = properties;
testMessageId = messageId;
});
这是我的验证电话:
mockTopic.Verify(m => m.EnqueueAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<DateTime?>(), It.IsAny<Dictionary<string, object>>(), It.IsAny<double>()));
这是verify方法抛出的错误信息:
Moq.MockException :
Expected invocation on the mock once, but was 0 times: m => m.EnqueueAsync<string>(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<DateTime?>(), It.IsAny<Dictionary<string, object>>(), It.IsAny<double>())
Performed invocations:
Mock<ITopic:1> (m):
IServiceBus<TopicClient>.EnqueueAsync<object>(null, null, "", "c0fa2b2a-fcb7-4252-964e-cecf97bbeeb9", null, True, null, Dictionary<string, object>, 1)
查看将验证定义与调用进行比较的消息,我发现调用之间没有区别。我对此束手无策。
【问题讨论】:
-
您期望的
messageObject的类型是什么?这只会匹配EnqueueAsync<string> -
字符串型
标签: c# .net unit-testing moq