Golang net package mocking

go, testing, unit-testing

Solution

Short answer is that you can't mock things like that. Go is a statically compiled language, so unlike ruby or other dynamic languages, there is no support for magically rewriting fixed function calls on the fly.

If you really want to test it, you have two obvious options:

Define `NewThingy` to take a function pointer whose type matches `net.InterfaceByName`. Pass that function to all "real" calls to the function, but pass a dummy function pointer in your tests. This will slightly slow down the code however, and isn't pleasant to read or write.

Require your test environment to use legitimate data. May or may not be possible depending on your requirements, but there are some packages that may help (I wrote https://github.com/eapache/peroxide for a similar purpose, but it doesn't deal with interfaces so it's probably not what you're looking for).

Problem

I'm trying to learn to write tests in golang on a personal project of mine. Bunch of the functions in my project are using `net` package, however since I'm new to this I don't know how to mock the functions from packages such as `net` one which are interacting with the host's devices. To make my question more concrete, I have a function similar to this: ``` func NewThingy(ifcName string) (*Thingy, error) { if ifc, err := net.InterfaceByName(ifcName); err == nil { return nil, fmt.Errorf("Interface name %s already assigned on the host", ifcName) } .... return &Thingy{ ifc: ifc, }, nil } ``` Thingy is clearly defined as: ``` type Thingy struct { ifc *net.Interface } ``` Anyone could give me any hints how to go about testing code like this ? Thanks

Original source