Skip to content

Instantly share code, notes, and snippets.

@tamanobi
Last active November 15, 2019 02:26
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 tamanobi/1ab869b22ff6980c61da5741b6bd15d1 to your computer and use it in GitHub Desktop.
Save tamanobi/1ab869b22ff6980c61da5741b6bd15d1 to your computer and use it in GitHub Desktop.
依存を注入できるようにしてテストする
package di
import (
"fmt"
"time"
)
func Hogehoge(fn func()) error {
// Do something(Heavy process)
time.Sleep(2 * time.Second)
go fn()
return nil
}
package di
import (
"sync"
"testing"
)
func TestHogehogeCallGoroutine(t *testing.T) {
// Goroutine が呼び出されていることをテストする
called := false
wg := &sync.WaitGroup{}
wg.Add(1)
Hogehoge(func() {
defer wg.Done()
called = true
})
wg.Wait()
if !called {
t.Errorf("関数が呼び出されていない")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment