C
C#•17mo ago
Obel

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
Obel
ObelOP•17mo ago
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
(Moq.Language.Flow.ISetup<HttpMessageHandler, Task<HttpResponseMessage>>)
(Moq.Language.Flow.ISetup<HttpMessageHandler, Task<HttpResponseMessage>>)
Like so, but that causes issues within the tests themselves
HimmDawg
HimmDawg•17mo ago
Does it have to be ReturnsForAnyArgs? That's from NSubstitute, not Moq
Obel
ObelOP•17mo ago
Not nescesarrily, this is just the approach i found and used for inspiration
HimmDawg
HimmDawg•17mo ago
Try .Returns(Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK))); then owo
Obel
ObelOP•17mo ago
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?
HimmDawg
HimmDawg•17mo ago
Can you show the test?
Obel
ObelOP•17mo ago
Yes sir,
Obel
ObelOP•17mo ago
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
HimmDawg
HimmDawg•17mo ago
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 itself
Obel
ObelOP•17mo ago
My login function works fine in Swagger
HimmDawg
HimmDawg•17mo ago
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 client
Obel
ObelOP•17mo ago
Ye i thought it would something wrong with the values being set incorrectly, thanks i'll have a look at it
Want results from more Discord servers?
Add your server