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
This is the test
which line throws this error?
The error is produced here
It says that the HttpRequestMessage request is null, im not sure how or if i can populate it with values?
what's It.IsAny
you need to construct an HttpRequestMessage first
what are you trying to do, sending a json data?
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
yeah what are u trying to send? did you even specify which URL to send data to?
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
https://github.com/richardszalay/mockhttp/blob/master/RichardSzalay.MockHttp.Tests/MockHttpMessageHandlerTests.cs This for example, its a different approach, with the URL like ur saying
GitHub
mockhttp/RichardSzalay.MockHttp.Tests/MockHttpMessageHandlerTests.c...
Testing layer for Microsoft's HttpClient library. Create canned responses using a fluent API. - richardszalay/mockhttp
And yes its Json data being sent
but right now in your code i dont see that you specified an URL
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
what does
It.IsAny<HttpRequestMessage>()
even return?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?
huh?
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
you're not getting HttpRequestMessage anywhere from the httpResponseMessage
and you should not construct HttpResponseMessage out of nowhere
Mmh i see, its not easy getting into. I think i might start from scratch to try and get a better understanding