type OsClient struct {}
func (o *OsClient) GetOSHostname() (string, error) { return os.Hostname() }
type HostnameGetter interface { GetOSHostname() (string, error)
}
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/mockAnd here are a couple other nice introductions:
On Thursday, August 1, 2019 at 7:09:54 AM UTC-5, Nitish Saboo wrote:
--
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...@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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/2f288e99-70db-476b-8a4a-75069be9e31a%40googlegroups.com.
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.
--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/mockAnd here are a couple other nice introductions:
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 unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@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.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/29CF1F67-E62C-410D-8DCF-9DFD75A10B62%40ix.netcom.com.
Tried running you code, it's failing:--- FAIL: TestF (0.00s)
test.go:43: got ubuntu, want testing
FAIL