November 2018
Intermediate to advanced
346 pages
8h 12m
English
If we are going to use DI and mocks, then the cleanest option is to mock the HTTP request so that we can make it return whatever response we need.
To achieve this, the first thing we need to do is abstract building and sending of HTTP requests, as shown in the following code:
// Requester builds and sending HTTP requests//go:generate mockery -name=Requester -case underscore -testonly -inpkg -note @generatedtype Requester interface { doRequest(ctx context.Context, url string) (*http.Response, error)}
You can see we have also included a go generate comment that will create the mock implementation for us.
We can then update our Converter to use the Requester abstraction, as shown in the following example: ...
Read now
Unlock full access