Last active
December 28, 2017 22:12
-
-
Save rogerwelin/077386141c53bcadd88972e1bc6a106c to your computer and use it in GitHub Desktop.
dependency injection mocking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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