[英]Problem while trying to unit test a method that uses a mocked Task<TResult>
I am trying to unit test a method that sends some notifications using FCM.This method takes a list of batches and for each batch it creates a Task of IBatchResponse where IBatchResponse is an interface I have made so I can mock the return type of the actual service sending the notification.我正在尝试对使用 FCM 发送一些通知的方法进行单元测试。此方法采用批次列表,并为每个批次创建一个 IBatchResponse 任务,其中 IBatchResponse 是我创建的接口,因此我可以模拟实际的返回类型发送通知的服务。 Then using Task.WhenAny() I am proccessing the tasks as they complete.
然后使用 Task.WhenAny() 我正在处理完成的任务。
public static async Task<NotificationRes> SendPushNotifications(List<Batch> batches, INotificationService notificationService)
{
var notificationTasks = new List<Task<IBatchResponse>>();
try
{
foreach (var batch in batches)
{
notificationTasks.Add(notificationService.SendNotification(guardTokens, "android"));
}
while (notificationTasks.Count > 0)
{
var finishedTask = await Task.WhenAny(notificationTasks);
var taskIndex = notificationTasks.FindIndex(task => task == finishedTask);
notificationTasks.Remove(finishedTask);
var finishedTaskResult = await finishedTask;
for (int i = 0; i < finishedTaskResult.Responses.Count; i++)
{
....
}
}
catch (Exception)
{
throw;
}
}
And here is the code for the test I am trying to make.I have mocked the wrapper service that sends the notification and the return type of notificationService.SendNotification (IBatchResponse).这是我正在尝试进行的测试的代码。我已经模拟了发送通知的包装服务和 notificationService.SendNotification (IBatchResponse) 的返回类型。
[Test]
public async Task SendPushNotifications_test()
{
...
var batchSendResponses = new List<ISendResponse>();
....
var mockNotifService = new Mock<INotificationService>();
var mockBatchRes = new Mock<IBatchResponse>();
mockBatchRes.Setup(res => res.Responses).Returns(batchSendResponses);
var task = Task.FromResult(mockBatchRes.Object);
mockNotifService.Setup(ns => ns.SendNotification(new string[5] { "token1", "token2", "", "", "" }, "android")).Returns(task);
var notRes = await Utils.SendPushNotifications(batches, mockNotifService.Object);
}
The problem is that if 2 batches exist the finishedTask Result is always null but if I have 1 batch it works.What am I missing here.问题是,如果存在 2 个批次,finishedTask Result 始终是 null 但如果我有 1 个批次,它可以工作。我在这里缺少什么。
I have found the problem which is in the mockNotifService.Setup()
method.我发现了
mockNotifService.Setup()
方法中的问题。
mockNotifService.Setup(ns => ns.SendNotification(new string[5] { "token1", "token2", "", "", "" }, "android")).Returns(task);
Currently in ns.SendNotification()
I am hardcoding the params and this seems to affect the Task returned or it doesn't produce the task I specify in .Returns()
and produces an empty one without the Result.目前在
ns.SendNotification()
我正在硬编码参数,这似乎会影响返回的任务,或者它不会产生我在.Returns()
中指定的任务并产生一个没有结果的空任务。 So I changed this to:所以我把它改成了:
mockNotifService.Setup(ns => ns.SendNotification(It.IsAny<string []>(), It.IsAny<string>())).Returns(Task.FromResult(mockBatchRes.Object));
and the test passed.并且测试通过了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.