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

sco*_*192 21 c# unit-testing moq

我正在尝试编写一个单元测试,我需要设置一个受保护的方法.我正在使用Moq进行此设置.

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

当该行执行时,它会抛出以下异常:

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

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

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

sco*_*192 34

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

以下是我在我的问题中尝试做的更正的实现:

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