Skip to content

Instantly share code, notes, and snippets.

@afeld
Created February 22, 2018 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afeld/ac2ea0702a9f8d2b91e55da4389e52d4 to your computer and use it in GitHub Desktop.
Save afeld/ac2ea0702a9f8d2b91e55da4389e52d4 to your computer and use it in GitHub Desktop.
example mocking in Golang
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