Skip to content

Instantly share code, notes, and snippets.

@bryanl
Created July 2, 2014 18:54
Show Gist options
  • Save bryanl/975a53365ac6786ff9ad to your computer and use it in GitHub Desktop.
Save bryanl/975a53365ac6786ff9ad to your computer and use it in GitHub Desktop.
package main
type KeyPair struct {
Key string
Value []byte
}
type KeyStore interface {
Get(k string) (*KeyPair, error)
}
type MockKeyStore struct {
Dict map[string][]byte
}
func NewRedisKeyStore() {
return &RedisKeyStore{
r: redis.Redis{},
}
}
func (rks *RedisKeyStore) Get(k string) (*KeyPair, error) {
rkvp, err := rks.r.Get(k)
if err != nil {
return nil, err
}
return &KeyPair{
Key: rkvp.Key,
Value: rkvp.Value,
}, nil
}
func NewMockKeyStore() {
return &MockKeyStore{
Dict: map[string][]byte{},
}
}
func (mks *MockKeyStore) Get(k string) (*KeyPair, error) {
return &KeyPair{Key: k, Value: mks.Dict[k]}, nil
}
@rugwirobaker
Copy link

This is a great piece for a beginner in programming and TDD. In all languages I have tried Go is the easiest to unit test and this just made it easier. Now my interfaces know what to do in my code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment