example is to stub out "time.Now" or "os.Hostname".
There seem to be 2 main options:
1) Create a Clock interface, and an implementation which uses the real
clock in production, but a fake clock in tests.
2) Create a var to refer to the global function, and replace that in tests.
e.g. var timeNow = time.Now
Option 1 is a lot of extra code, and I don't like to use interfaces when
there's only one real implementation used in production code.
Option 2 works, but it's a little ugly as your code no longer uses the
function directly, so you'll see "timeNow()" instead of "time.Now()". This
makes it harder to search code, and it allows for the timeNow var to be
changed in production code, which I never actually want.
It would be great if there was a better solution to this problem. I've
thought of a couple of options:
1) Allow functions to be stubbed only during tests. Func declarations could
be treated as var FuncName = func(...)
The main problem I see with this is that a separate build of the standard
library may need to be used to achieve this.
2) Rewrite code for test builds that will create the variable "var timeNow
= time.Now" and use the variable instead of the real function and allow it
to be stubbed, but only for tests.
I think 2) would work better if a stubbing library (something like gostub
<https://github.com/prashantv/gostub>) was part of "testing" in the
standard library, as then calls to a Stub function could be recognized by
"go test" and replaced to use a var.
It'd be great to hear feedback or other ideas for this problem, thanks.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/d/optout.