Testing CLI commands
While building your command-line application, it’s important to also build testing around it so you can ensure that the application works as expected. There are a few things that typically need to be done, including the following:
- Mock the HTTP client
- Handle test configuration
- Create a test for each command
We’ll go over the code for each of these steps that exist in the audio file repository for Chapter 11.
Mocking the HTTP client
To mock the HTTP client, we’ll need to create an interface to mimic the client’s Do
method, as well as a function that returns this interface, which is both satisfied by the real and mocked client.
In the cmd/client.go
file, we’ve written some code to handle all of this:
type AudiofileClient interface { Do(req *http.Request) (*http.Response, error) } var ( getClient = GetHTTPClient() ) func GetHTTPClient() AudiofileClient...