On Sat, 3 Aug 2019 22:35:44 -0700 (PDT)
Andrey Tcherepanov wrote:
> what if you need only a subpart of the package to be mocked ?
import smthorig "smth"
type smthT struct{
//functions
f1 func signature
f2 func signature
}
// functions
func mof1 (in...) (out...) { /* here your mock goes */ }
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
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
> wrote:
>
> what if you need only a subpart of the package to be mocked ?
>
>> On
This can also be done without an interface:
https://play.golang.org/p/fgRX2nXIxn0
In ^ example “Thing” would be the type you are working on, with functional
options
(https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis
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.b...@sezzle.com
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
You'll want to look into gomock and mockgen. To get started mocking,
you'll need an interface, so maybe writing a receiver function
encapsulating os.Hostname(), then consuming that with an interface.
type OsClient struct {}
func (o *OsClient) GetOSHostname() (string, error) { return