
HTTP CLIENT TEST HOW TO
Today in this post we will see how to mock only Typed HTTPClient using the HttpClientFactory interface. HttpClientFacory interface lets you create HTTPClient objects like using Named HTTPClient and Typed HTTPClient. NET Core as it provides multiple benefits which we discussed in our article on HTTPClient Vs HTTPClientfactory. HTTTPClient object can be created using HttpClientFactory in. In our last article, we already learned how to mock basic HTTPClient and Named HTTPClient using HTTPClientFactory. In this article, we will see how to Unit Test and Mock Typed HTTPClient request objects created using IHttpClientFactory interfaces with proper moq mocking in. Providing canned responses (maybe even from file via net/http.ReadResponse) is simple and often leads to much clearer tests than abstruse mocks.Unit Test and Mock Typed HttpClient using HttpClientFactory. The burden now lies on writing a fake or stub. Starting a httptest.Server is fast, requires no external networking and works reliable. This looks strange if you where raised in the church of "unit testing must abstract away each and any dependency no matter what and and why no arguing here!".
HTTP CLIENT TEST CODE
You can test any behaviour/reaction of you HTTPClient or code using it. No interfaces, no mocks, no generated code, no hassle. When done with your test: Stop the server again. In the test: inject the into your HTTPClient and use that client. In the test start a local HTTP server with net/http/httptest.Server wired to your stub or fake. Write a net/http.HandlerFunc which provides a stub or fake for your HTTP endpoint. It needs some kind of base URL(s)/hostnames whatever configuring where to it'll make its HTTP requests. Have a concrete HTTPClient struct which does the work. There are more test doubles than mocks and for your case a fake or stub would be much simpler, you even might not need an interface at all to allow using a test double. In Go mocking is rarely a good test strategy. Thats why everybody learns to mock, there are mock frameworks for any language and endless tutorials and courses explaining that unit-testing and mocking go hand in hand like siamese twins.

Mocking is the testing strategy because it is basically the only testing strategy in traditional OOP which works at all if you want to do extrem low-level unit-testing. Hopefully i explained correctly i can add code if needed im just on my phone rn so will need to be on my laptop to add it.ĭo not mock. My main question: Is there a better way to do this? Should I be going about this a completely different way or is gomock/mockgen the way to go and i’m just not implementing them correctly? I have been using the Gomock framework and i also tried mockgen - neither works. My issue comes when i try to pass the MockClient struct, which has a Do func so therefore implementing the HTTPClient interface, into function A i get an error saying it’s not the correct type. I have a function, I’ll call it function A that takes the HTTPClient interface as an argument then calls the Do func within it. I then create a variable of HTTPClient and set it equal to &http.Client. I created an HTTPClient interface with a Do func that is the exact same signature as the.

I almost had the following working but i get an error: I feel like I’ve tried everything and nothing seems to work.
