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 golang-nuts+unsubscr...@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.

Reply via email to