C
C#12mo ago
Obel

HttpRequestMessage request cannot be null, TESTING

I will attach the 2 relevant code snippets. Simply the test fails because the request cannot be null. And im not sure how to go around it. Thanks for any help 🙂
17 Replies
Obel
Obel12mo ago
This is the test
TheRanger
TheRanger12mo ago
which line throws this error?
Obel
Obel12mo ago
The error is produced here
_httpClient.SendAsync(It.IsAny<HttpRequestMessage>(), default)
_httpClient.SendAsync(It.IsAny<HttpRequestMessage>(), default)
It says that the HttpRequestMessage request is null, im not sure how or if i can populate it with values?
TheRanger
TheRanger12mo ago
what's It.IsAny you need to construct an HttpRequestMessage first what are you trying to do, sending a json data?
Obel
Obel12mo ago
I want my expectedResult to have a token, and then when i do the call for the actual value, that should also include a token
TheRanger
TheRanger12mo ago
yeah what are u trying to send? did you even specify which URL to send data to?
Obel
Obel12mo ago
I have been using chatgpt and copilot, for guidance on how to mock Httpclient and basicly testing in general And i've found another github that also uses a mock approach, it looks different tho, and i have a hard time figuring out what would work and what doesnt
Obel
Obel12mo ago
GitHub
mockhttp/RichardSzalay.MockHttp.Tests/MockHttpMessageHandlerTests.c...
Testing layer for Microsoft's HttpClient library. Create canned responses using a fluent API. - richardszalay/mockhttp
Obel
Obel12mo ago
And yes its Json data being sent
TheRanger
TheRanger12mo ago
but right now in your code i dont see that you specified an URL
Obel
Obel12mo ago
Right because im faking the Token in my expected result right, and then the _loginService calls the Login function which actually contacts a URL Im not used to testing, its my first time trying to test real functionality
TheRanger
TheRanger12mo ago
what does It.IsAny<HttpRequestMessage>() even return?
Obel
Obel12mo ago
Its supposed to return the result from the httpResponseMessage, which is made just above. Which should be a token Should i try a totally different approach, and just try to discard this one i have? Is there an easier way of unit testing functionality?
TheRanger
TheRanger12mo ago
huh?
Obel
Obel12mo ago
Thats what im saying too. I think i got overwhelmed with this way of having to mock and test functionality, that i tried something without completely understanding what it was doing
TheRanger
TheRanger12mo ago
you're not getting HttpRequestMessage anywhere from the httpResponseMessage and you should not construct HttpResponseMessage out of nowhere
Obel
Obel12mo ago
Mmh i see, its not easy getting into. I think i might start from scratch to try and get a better understanding