[英]Expected invocation on the mock exactly 5 times, but was 0 times with properly mocked arguments
通过阅读这篇文章和这篇文章,我了解到应该使用Mock
并且应该等待结果。
尽管如此,我仍然得到以下异常:
Moq.MockException:预期对模拟的调用恰好 5 次,但为 0 次:_ =>
_.SendAsync(Mock<IClientProxy:2>.Object, It.IsAny<string>(), It.IsAny<List<Notification>>(), CancellationToken)
执行的调用:
MockIClientContextCommand:2 (_):
IClientContextCommand.SendAsync(MockIClientProxy:2.Object, "FooBarNotifications", [Notification], CancellationToken) IClientContextCommand.SendAsync(MockIClientProxy:2.Object, "FooBarNotifications", [Notification], CancellationToken) IClientContextCommand.SendAsync(MockIClientProxy:2.Object, "FooBarNotifications", [Notification ], CancellationToken) IClientContextCommand.SendAsync(MockIClientProxy:2.Object, "FooBarNotifications", [Notification], CancellationToken) IClientContextCommand.SendAsync(MockIClientProxy:2.Object, "FooBarNotifications", [Notification], CancellationToken) IClientContextCommand.SendAsync(MockIClientProxy:2.Object, "FooBarNotifications ", [通知], CancellationToken) IClientContextCommand.SendAsync(MockIClientProxy:2.Object, "FooBarNotifications", [通知], CancellationToken) IClientContextCommand.SendAsync(MockIClientProxy:2.Object, "FooBarNotifications", [通知], CancellationToken) IClientContextCommand.SendAsync(MockIClientProxy:2.Object,“FooBarNotifications”,[通知],CancellationToken)
堆栈跟踪:Mock.Verify(Mock mock, LambdaExpression expression, Times times, String failMessage)line337 Mock
1.Verify[TResult](Expression
1 expression, Times times)line34 FooBarServiceTests.SendAsync_ShouldBe_Called_N_Times()line105
所以单元测试看起来像这样:
private readonly Mock<IClientContextCommand> _mockClientContextCommand;
// constructor
public FooTests()
{
_mockClientContextCommand = new Mock<IClientContextCommand>();
_mockClientContextCommand.Setup(x =>
x.SendAsync(_mockClientProxy.Object, It.IsAny<string>(),
It.IsAny<Notification[]>(), CancellationToken.None));
services.AddSingleton(_mockClientContextCommand.Object);
}
[Fact]
public async Task SendAsync_ShouldBe_Called_N_Times()
{
await _hostedService.StartAsync(CancellationToken.None);
await Task.Delay(1);
await _hostedService.StopAsync(CancellationToken.None);
int times = GetNotifications().Count();
_mockClientContextCommand.Verify(
_ => _.SendAsync(_mockClientProxy.Object, It.IsAny<string>(),
It.IsAny<List<Notification>>(), CancellationToken.None),
Times.Exactly(times));
}
原始方法如下所示:
private async Task Send(CancellationToken stoppingToken)
{
// ... the other code is omitted for the brevity
foreach (IGrouping<string, Notification> group in messages.GroupBy(m => m.To))
{
IClientProxy connection = _hubContext.Clients.User(group.Key);
List<Notification> notifies = group.ToList();
await _clientContextCommand.SendAsync(connection,
"FooBarNotifications", notifies, stoppingToken);
}
}
接口方法如下所示:
public interface IClientContextCommand
{
Task SendAsync(IClientProxy clientProxy, string method,
object? arg1, CancellationToken cancellationToken = default);
}
有谁知道做错了什么? 任何帮助将不胜感激
首先,您需要使用It.IsAny
成员的匹配参数,以便调用模拟成员。 由于您的设置/验证与实际调用的内容不匹配,因此测试失败。
接下来,因为这是一个似乎没有返回值的异步成员,所以该成员需要返回一个已完成的任务以允许异步代码流向完成。
//... omitted for brevity
_mockClientContextCommand
.Setup(_ => _.SendAsync(It.IsAny<IClientProxy>(), It.IsAny<string>(), It.IsAny<Notification[]>(), It.IsAny<CancellationToken>()))
.Returns(Task.CompletedTask);
//... omitted for brevity
int times = GetNotifications().Count();
_mockClientContextCommand.Verify(
_ => _.SendAsync(It.IsAny<IClientProxy>(), It.IsAny<string>(), It.IsAny<Notification[]>(), It.IsAny<CancellationToken>()),
Times.Exactly(times));
堆栈跟踪还显示预期计数应该为 5,但显示了 10 次SendAsync
调用。 一旦您获得模拟以匹配预期的调用,这可能需要进一步调查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.