Golang网络软件包模拟

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

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).

This isn't a direct answer to your question, but Gomock provides for mocking by generating code for whatever interface you give it. To use it, you need to use an interface-based coding style, rather than relying directly on structs.

Because many of the standard library packages use interfaces, mocking them with Gomock is effective. For any that don't, you might need to write an (untested) shim that provides the necessary interface layer. This seems less messy to me than the idea of passing function pointers to achieve a similar effect.