C
C#•2y 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
ObelOP•2y ago
This is the test
TheRanger
TheRanger•2y ago
which line throws this error?
Obel
ObelOP•2y 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
TheRanger•2y ago
what's It.IsAny you need to construct an HttpRequestMessage first what are you trying to do, sending a json data?
Obel
ObelOP•2y 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
TheRanger•2y ago
yeah what are u trying to send? did you even specify which URL to send data to?
Obel
ObelOP•2y 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
ObelOP•2y 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
ObelOP•2y ago
And yes its Json data being sent
TheRanger
TheRanger•2y ago
but right now in your code i dont see that you specified an URL
Obel
ObelOP•2y 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
TheRanger•2y ago
what does It.IsAny<HttpRequestMessage>() even return?
Obel
ObelOP•2y 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
TheRanger•2y ago
huh?
Obel
ObelOP•2y 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
TheRanger•2y ago
you're not getting HttpRequestMessage anywhere from the httpResponseMessage and you should not construct HttpResponseMessage out of nowhere
Obel
ObelOP•2y ago
Mmh i see, its not easy getting into. I think i might start from scratch to try and get a better understanding

Did you find this page helpful?