Delegate type mismatch
Im currently trying to test my project, and im trying to mock an HttpClient, but im running into some errors. I have attached a code snippet of the function and the error, thank you in advance 🙂
12 Replies
I've been trying to look for an answer for some time, but i just cant wrap my head around it..
For example i have tried explicitly casting the type Like so, but that causes issues within the tests themselves
Does it have to be
ReturnsForAnyArgs
? That's from NSubstitute, not MoqNot nescesarrily, this is just the approach i found and used for inspiration
Try
.Returns(Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)));
then Thanks, i'll have a look at that
Mmh thanks, it seems to have fixed atleast that part. Looking into fixing the unit test now! 🙂
Seems that my request in the SendAsync is null with this, any idea why?
Can you show the test?
Yes sir,
Could it be, because im using .ReturnsForAnyArgs?
It is the HttpRequestMessage request, that is null in this case
And it should not be
And that comes from the _httpClient.SendAsync
Well, maybe your
_loginService.Login()
has a flaw
But other than that, ReturnsForAnyArgs is meant to be used with mocks. Pretty sure you cannot use that on the httpClient itselfMy login function works fine in Swagger
Then it's gonna be the latter
Take
RichardSzalay.MockHttp
for example. You have to set the returned values on the mocked HttpMessageHandler
itself rather than on the clientYe i thought it would something wrong with the values being set incorrectly, thanks i'll have a look at it