[英]Using Moq Unable to Verify Mocked Call With Default Parameters
Attempting to call verify on a Mocked dependency always fails no matter how general I make the Setup.无论我如何设置安装程序,尝试对 Mocked 依赖项调用 verify 总是失败。
Here is the method signature on the interface I am hoping to mock:这是我希望模拟的接口上的方法签名:
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);
Here is my Setup method:这是我的设置方法:
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;
});
Here is my call to verify:这是我要验证的电话:
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>()));
And this is the error message that is thrown from the verify method:这是从 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)
Looking at the message comparing the definition of the verify to the invocation, I see no difference between the calls.查看将验证的定义与调用进行比较的消息,我发现调用之间没有区别。 I'm at wits end with this.我对此无能为力。
You can use It.IsAnyType
for this:您可以为此使用It.IsAnyType
:
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Moq;
namespace ConsoleApp24
{
class Program
{
static void Main(string[] args)
{
var mock = new Mock<ITopic>();
var ensureAsyncSetup = mock.Setup(m => m.EnqueueAsync(
It.IsAny<string>(),
It.IsAny<string>(),
// By using It.IsAnyType the matcher will work for all T
It.Is<It.IsAnyType>((v, _) => true),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<bool>(),
It.IsAny<DateTime?>(),
It.IsAny <Dictionary<string, object>>(),
It.IsAny<double>()));
ensureAsyncSetup.Callback((string factoryId, string clientId, object messageObject, string messageId,
string messageLabel, bool forcePersistence, DateTime? scheduledEnqueueTimeUtc,
Dictionary<string, object> properties, double retryTime) =>
{
Console.WriteLine("This will run");
});
// Setup call as verifiable
ensureAsyncSetup.Verifiable();
mock.Object.EnqueueAsync(null, null, "", "c0fa2b2a-fcb7-4252-964e-cecf97bbeeb9", null, true, null, new Dictionary<string, object>(), 1);
// This will pass as the method is called once
mock.Verify();
}
}
public interface ITopic
{
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.