Last active
November 15, 2019 02:26
-
-
Save tamanobi/1ab869b22ff6980c61da5741b6bd15d1 to your computer and use it in GitHub Desktop.
依存を注入できるようにしてテストする
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
package di | |
import ( | |
"fmt" | |
"time" | |
) | |
func Hogehoge(fn func()) error { | |
// Do something(Heavy process) | |
time.Sleep(2 * time.Second) | |
go fn() | |
return nil | |
} |
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
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