最小起订量此设置与匹配呼叫的设置不匹配

Moq this setup was not matched for setup that matches the calls

正在测试的服务中有如下代码:

public SendNotificationAsync(SomeDataType payloadData)
{
    ...

    var androidPayload = new AndroidNotification
    {
        Data = payloadData,
    };
    await _hubClient.SendFcmNativeNotificationAsync(androidPayload, tags);

    ...
}

_hubClient 是注入到此服务中的 INotificationHubClient 接口的一个实例。

测试的最小起订量设置如下:

private readonly Mock<INotificationHubClient> _hubClient = new(MockBehavior.Strict);
public Task TestMethod()
{
    var _notificationService = new NotificationService(_hubClient.object);
    ...

    _hubClient
        .Setup(s => s.SendFcmNativeNotificationAsync(It.Is<AndroidNotification>(a => a == androidPayload), It.Is<string[]>(s => s == stringArray)))
        .ReturnsAsync(retVal)
        .Verifiable();

    ...

    await _notificationService.SendNotificationAsync(payloadData);

    _hubClient.Verify();
}

当我 运行 测试时,出现以下错误:

Moq.MockException: Mock<INotificationHubClient:1>: This mock failed verification due to the following:
INotificationHubClient s => s.SendFcmNativeNotificationAsync(It.Is<AndroidNotification>(a => a == AndroidNotification), It.Is<string[]>(s => s == ["userName1" , "userName2"])): This setup was not matched.

当我使用 It.IsAny<AndroidNotification>() 时测试通过了,但我想检查传入的值,这就是为什么我更喜欢与当前使用的方法类似的方法。

显然错误与不正确的引用相等性检查有关,测试中传递的第二个 arg 参数是 string[] 类型,而该方法需要 IEnumerable<string> 才能通过,因为 Moq 正在检查在这种情况下是特定类型。