Skip to content

Instantly share code, notes, and snippets.

@yamamoto-febc
Last active February 22, 2018 12:41
Show Gist options
  • Save yamamoto-febc/6ce1e47a67e0b318254abae31f892f65 to your computer and use it in GitHub Desktop.
Save yamamoto-febc/6ce1e47a67e0b318254abae31f892f65 to your computer and use it in GitHub Desktop.
テスト用にこんな感じのジェネレーターを探してる
/*
* こんなインターフェースを与えると
*/
type Foobar interface {
List() []*Foo
Read(string) (*Foo, error)
}
/*
* こんなソースを生成して欲しい
*/
type testFoobar struct {
listResult []*Foo
readResult *Foo
readError error
}
func (t *testFoobar) List() []*Foo {
return t.listResult
}
func (t *testFoobar) Read(p1 string) (*Foo , error) {
return t.readResult, t.readError
}
/*
* こんな感じでテストに使いたい
*/
func TestFoobarUser_Read(t *testing.T) {
// テスト対象、Foobarインターフェースを利用する
var foobarUser := &foobarUser{}
t.Run("Foobar returns error" , func(t *testing.T){
expectError := errors.New("test")
// テスト用のハンドラを設定
foobarUser.handler = &testFoobar{
readError: expectError // Readを呼んだ時に返すエラー
}
// Readの内部ではテストハンドラが利用される
result , err := foobarUser.Read(testID)
assert.Nil(t , result)
assert.Error(t , err)
assert.Equal(t , expectError, err)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment