Beniamino_Baggins Beniamino_Baggins - 1 year ago 90
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
Visual studio before compilation:

expected a method with
'bool IsAny(HttpResponseMessage)'

What am I doing wrong?

Answer Source

Remove the Expression in

    .Setup(sm => sm.CreateHttpClientForJwt(It.IsAny<Expression<Func<HttpResponseMessage, bool>>>, 5))

and update the It.IsAny to

    .Setup(sm => sm.CreateHttpClientForJwt(It.IsAny<Func<HttpResponseMessage, bool>>(), 5))

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<>>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download