使用 ItExpr.IsNull<TValue> 而不是空参数值,因为它会阻止正确的方法查找

Use ItExpr.IsNull<TValue> rather than a null argument value, as it prevents proper method lookup

我正在尝试编写需要设置受保护方法的单元测试。我正在为此设置使用最小起订量。

_innerHandler.Protected()
             .Setup<Task<HttpResponseMessage>>("SendAsync", It.IsAny<HttpRequestMessage>(), It.IsAny<CancellationToken>())
             .ReturnsAsync(responseMessage);

当这行代码执行时,抛出以下异常:

System.ArgumentException : Use ItExpr.IsNull<TValue> rather than a null argument value, as it prevents proper method lookup.

当我将其更改为使用 ItExpr.IsNull<TValue> 时,它确实允许执行测试。但是,它当然缺少我想要配置的设置。

我需要做什么才能使用 It.IsAny<TValue> 设置受保护的方法?

设置 IProtectedMock 时,您应该使用 Moq.Protected.ItExpr 而不是 Moq.It

这是我在问题中尝试做的更正实施:

_innerHandler.Protected()
             .Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
             .ReturnsAsync(responseMessage);