[英]Using Moq Unable to Verify Mocked Call With Default Parameters
無論我如何設置安裝程序,嘗試對 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)
查看將驗證的定義與調用進行比較的消息,我發現調用之間沒有區別。 我對此無能為力。
您可以為此使用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.