Skip to content

Instantly share code, notes, and snippets.

@rogerwelin
Last active December 28, 2017 22:12
Show Gist options
  • Save rogerwelin/077386141c53bcadd88972e1bc6a106c to your computer and use it in GitHub Desktop.
Save rogerwelin/077386141c53bcadd88972e1bc6a106c to your computer and use it in GitHub Desktop.
dependency injection mocking
package main
import (
"bytes"
"io/ioutil"
"net/http"
"testing"
)
type MockHttpClient struct{}
func (m *MockHttpClient) Get(url string) (*http.Response, error) {
response := &http.Response{
Body: ioutil.NopCloser(bytes.NewBuffer([]byte("Test response"))),
}
return response, nil
}
func (m *MockHttpClient) Post(url string, contentType string, body io.Reader) (*http.Response, error) {
response := &http.Response{
Body: ioutil.NopCloser(bytes.NewBuffer([]byte("Response"))),
}
return response, nil
}
func TestSendWithValidResponse(t *testing.T) {
httpClient := &MockHttpClient{}
err := send(httpClient, "IT_JUST_WORKS!")
if err != nil {
t.Errorf("Shouldn't have received an error with a valid MockHttpClient, got %s", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment