Similarly the fact that you need to mock just os.Hostname and you know that means you are probably testing implementation details which I wouldn’t do. As I’ve pointed out before most of the Go stdlib tests are black box in a different test package. I think you’ll create better more resilient tests that way.
> On Aug 4, 2019, at 6:23 AM, Robert Engels <reng...@ix.netcom.com> wrote: > > In an ideal case got exported functions it would defer to the original. > > Still I think the package might have too many responsibilities if that’s the > case. > >> On Aug 4, 2019, at 12:35 AM, Andrey Tcherepanov >> <xnow4fippy...@sneakemail.com> wrote: >> >> what if you need only a subpart of the package to be mocked ? >> >>> On Thursday, August 1, 2019 at 5:29:30 PM UTC+3, Robert Engels wrote: >>> That brings up an interesting idea. A ‘package replace’ during compile. So >>> you request that the os package is replaced by osmock etc. >>> >>> This would allow easy general reuse mocking frameworks without changing the >>> stdlib and all user code. >>> >>>> On Aug 1, 2019, at 8:38 AM, kyle...@sezzle.com wrote: >>>> >>>> You'll want to check out `gomock` and `mockgen`. You'll need to write an >>>> interface to be able to mock, so you may need to write a receiver struct >>>> to encapsulate os.Hostname() or whatever you need to mock, then mock your >>>> HostnameGetter (surely a better name than that), maybe? >>>> >>>> type OsClient struct {} >>>> >>>> (o *OsClient) GetHostname() (string, error) { return os.Hostname() } >>>> >>>> type HostnameGetter interface { GetHostname() (string, error)} >>>> >>>> >>>> >>>> Here is repo w/ documentation: https://github.com/golang/mock >>>> >>>> And here are a couple other nice introductions: >>>> - https://blog.codecentric.de/en/2017/08/gomock-tutorial/ >>>> - https://www.philosophicalhacker.com/post/getting-started-with-gomock/ >>>> >>>>> On Thursday, August 1, 2019 at 7:09:54 AM UTC-5, Nitish Saboo wrote: >>>>> Hi, >>>>> >>>>> How can we mock a function in Go like os.Hostname() or os.Getwd() ? >>>>> Any framework or Library that we can use for mocking the function calls ? >>>>> >>>>> Thanks, >>>>> Nitish >>>> >>>> -- >>>> 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 golan...@googlegroups.com. >>>> To view this discussion on the web visit >>>> https://groups.google.com/d/msgid/golang-nuts/bceaef08-4ee2-4f57-8c0d-5fee59e8cfd7%40googlegroups.com. >> >> -- >> 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 golang-nuts+unsubscr...@googlegroups.com. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/golang-nuts/1e62434b-d7b6-403e-87a8-d0edf42d3dc0%40googlegroups.com. > -- > 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 golang-nuts+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/29CF1F67-E62C-410D-8DCF-9DFD75A10B62%40ix.netcom.com. -- 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 golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/50AE5174-CC06-472B-8AE9-C3E7420DB6BB%40ix.netcom.com.