Beniamino_Baggins Beniamino_Baggins - 13 days ago 8
C# Question

Moq error - expected a method with 'bool IsAny(HttpResponseMessage)'

I want to mock this method:

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));
}


my attempt:

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


I'm getting this error in on the
IsAny
Visual studio before compilation:


expected a method with
'bool IsAny(HttpResponseMessage)'



What am I doing wrong?

Answer

Remove the Expression in

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

and update the It.IsAny to

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

It.IsAny<TValue>() is a method but you were calling it like a property. You also described the method as expecting a Func but setup with an Expression<Func<>>