I'm trying to test the following method in a file (heavily edited to make a small sample):
app.go
var fruit = ""
func check(input string) bool {
if input == "1" {
go func() {
fruit = "BANANA"
time.Sleep(1 * time.Second)
}()
return true
} else if fruit == "BANANA" {
return true
}
return false
}
app_test.go
func TestInputs(t *testing.T) {
res := check("1")
res = check("2")
if res != true {
t.Errorf("got: %t, want: %t", res, true)
}
}
Essentially, I'd like res = check("2")
to return true, since fruit was set to BANANA in the previous call. Is this possible? The Go Routine shouldn't matter too much here - however it is part of my program's functionality and I'm not sure if it affects testing.