I've been thinking about this whole night, but still cannot find an elegant way to do this thing. Let's say I have a struct
type file struct {
x int
}
func (f *file) filename() string {
return fmt.Sprintf("%s/%d.log", exportPath, f.x)
}
func (f *file) write(data []byte) {
...
aFile = os.File.Open(f.filename())
...
}
Now I want to test write method and stub filename method to return temp filename. How can I do this? To the moment I found two options:
- declare filename = func(f* file) and override it in test
- make filename a field of the struct
But they both seem wrong in this case. So the question is - can I stub in any way this method? And in general - how to stub internal methods for testing (for external obviously dependency injection could work)