Skip to content

Instantly share code, notes, and snippets.

@karlseguin
Created January 7, 2014 10:07
Show Gist options
  • Save karlseguin/8297287 to your computer and use it in GitHub Desktop.
Save karlseguin/8297287 to your computer and use it in GitHub Desktop.
// x.go
package di
var someCall = func() string {
return "original implementation"
}
func SomeMethodToTest() string {
return someCall()
}
// x_test.go
package di
import (
"testing"
)
func TestSomeCall(t *testing.T) {
someCall = func() string {
return "mock"
}
if SomeMethodToTest() != "mock" {
t.Fail()
}
}
@webdev
Copy link

webdev commented Jan 29, 2014

Wouldn't this be a problem, if another test in the same package happens to override someCall method?

Redefining someCall method is not scope protected if it's in the same package?

@pavelnikolov
Copy link

I wonder how would you test dependencies from other packages - for example you have a web handler which calls a data layer methods... How do you manage dependency injection in such cases?

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