NSubstitute icon indicating copy to clipboard operation
NSubstitute copied to clipboard

NSubstitute.Exceptions.CouldNotSetReturnDueToTypeMismatchException

Open gsmittal opened this issue 4 years ago • 1 comments

I am using NSubstitute for unit testing. I am getting below exception for ReturnsForAnyArgs Exception thrown: 'NSubstitute.Exceptions.CouldNotSetReturnDueToTypeMismatchException' in NSubstitute.dll

        this.exchangeService = Substitute.For<IExchangeService>();
        HttpClient httpClient = new HttpClient();
        this.exchangeService.Client.Returns(httpClient);
        HttpResponseMessage httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK);
        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, this.applicationContext.SubstrateRestApiUrl);
        this.exchangeService.Client.SendAsync(request).ReturnsForAnyArgs(Task.FromResult(httpResponseMessage));

gsmittal avatar Jul 29 '21 18:07 gsmittal

Hi @gsmittal ,

Sorry for the late response; this notification got lost in my inbox. 😓

Have you added the NSubstitute.Analyzers package to your test project? This is probably the easiest way to find the cause of these problems.

In this case, exchangeService.Client is set to a real HttpClient, so maybe exchangeService.Client.SendAsync is failing due to this?

dtchepak avatar Aug 17 '21 05:08 dtchepak

Closing this due to inactivity. Please let us know if this still an issue and please provide the requested info - thanks!

304NotModified avatar Apr 29 '24 12:04 304NotModified