Created
February 22, 2018 19:46
-
-
Save afeld/ac2ea0702a9f8d2b91e55da4389e52d4 to your computer and use it in GitHub Desktop.
example mocking in Golang
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
type APIClientIface interface { | |
CreateAccountInOrgUnit() bool | |
} | |
type RealAPIClient struct {} | |
func (ac RealAPIClient) CreateAccountInOrgUnit() bool { | |
// ... | |
} | |
type Manager struct { | |
client APIClientIface // RealAPIClient | |
} | |
func (m Manager) CreateAccount() { | |
m.client.CreateAccountInOrgUnit() | |
// ... | |
} | |
// real code | |
client := APIClient{} | |
mgr := Manager{ | |
client: client, | |
} | |
mgr.CreateAccount() | |
// tests | |
type FakeAPIClient struct {} | |
func (ac FakeAPIClient) CreateAccountInOrgUnit() bool { | |
return true | |
} | |
fakeClient := FakeAPIClient{} | |
mgr := Manager{ | |
client: fakeClient, | |
} | |
mgr.CreateAccount() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment