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