Moq错误 - 期望使用'bool IsAny(HttpResponseMessage)'的方法

时间:2016-11-24 21:00:03

标签: c# .net unit-testing moq

我想模仿这个方法:

public HttpClient CreateHttpClientForJwt(Func<HttpResponseMessage, bool> isUnauthenticated, int timeoutSeconds)
{
    var client = Mvx.Resolve<IPlatformOperationProvider>().CreateHttpClient(timeoutSeconds);
    return new HttpClient(new AuthenticatedHttpMessageHandler(this, client, AuthenticationUtils.AddJwtToRequest, isUnauthenticated,_loggingService));
}

我的尝试:

authenticatedRequestServiceMock.Setup(sm => sm.CreateHttpClientForJwt(It.IsAny<Expression<Func<HttpResponseMessage, bool>>>, 5)).Returns(stubHttpClient.Object);

我在编译之前在IsAny Visual studio上收到此错误:

  

期望使用'bool IsAny(HttpResponseMessage)'

的方法

我做错了什么?

1 个答案:

答案 0 :(得分:1)

删除

中的表达式
authenticatedRequestServiceMock
    .Setup(sm => sm.CreateHttpClientForJwt(It.IsAny<Expression<Func<HttpResponseMessage, bool>>>, 5))
    .Returns(stubHttpClient.Object);

并将It.IsAny更新为

authenticatedRequestServiceMock
    .Setup(sm => sm.CreateHttpClientForJwt(It.IsAny<Func<HttpResponseMessage, bool>>(), 5))
    .Returns(stubHttpClient.Object);

It.IsAny<TValue>()是一种方法,但您将其称为属性。您还将该方法描述为期望Func,但使用Expression<Func<>>

进行设置