Golang的net包模拟

huangapple go评论108阅读模式
英文:

Golang net package mocking

问题

我正在尝试学习在我的个人项目中使用golang编写测试。

我的项目中有很多函数使用了net包,但是由于我对此还不熟悉,我不知道如何模拟与主机设备交互的net包中的函数。

为了让我的问题更具体,我有一个类似于这样的函数:

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明确定义为:

type Thingy struct {
  ifc *net.Interface
}

有人可以给我一些关于如何测试这样的代码的提示吗?

谢谢

英文:

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

答案1

得分: 3

简短回答是你无法模拟这样的东西。Go是一种静态编译语言,所以与Ruby或其他动态语言不同,它不支持在运行时神奇地重写固定的函数调用。

如果你真的想测试它,有两个明显的选项:

  • 定义NewThingy以接受一个函数指针,其类型与net.InterfaceByName匹配。将该函数传递给所有对该函数的“真实”调用,但在测试中传递一个虚拟函数指针。这会稍微减慢代码的执行速度,并且阅读和编写起来不太方便。

  • 要求你的测试环境使用合法的数据。根据你的要求可能有可能也可能没有可能,但有一些包可能会有所帮助(我为类似的目的编写了https://github.com/eapache/peroxide,但它不处理接口,所以可能不是你要找的)。

英文:

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

答案2

得分: 1

这不是对你问题的直接回答,但是Gomock通过为给定的接口生成代码来提供模拟功能。要使用它,你需要使用基于接口的编码风格,而不是直接依赖于结构体。

因为许多标准库包使用接口,使用Gomock来模拟它们是有效的。对于那些不使用接口的包,你可能需要编写一个(未经测试的)shim来提供必要的接口层。对我来说,这似乎比传递函数指针来实现类似效果的想法更清晰。

英文:

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.

答案3

得分: 1

我遇到了同样的问题,最终使用了这个包来进行模拟:
https://github.com/foxcpp/go-mockdns

srv, _ := mockdns.NewServer(map[string]mockdns.Zone{
    "example.org.": {
        A: []string{"1.2.3.4"},
    },
}, false)
defer srv.Close()

srv.PatchNet(net.DefaultResolver)
defer mockdns.UnpatchNet(net.DefaultResolver)

addrs, err := net.LookupHost("example.org")
fmt.Println(addrs, err)

它还提供了创建模拟解析器对象的选项,但是你需要使用依赖注入将模拟解析器注入到内部使用:

r := mockdns.Resolver{
    Zones: map[string]mockdns.Zone{
        "example.org.": {
            A: []string{"1.2.3.4"},
        },
    },
}

addrs, err := r.LookupHost(context.Background(), "example.org")
fmt.Println(addrs, err)
英文:

I had the same issue and ended up using this package to mock:
https://github.com/foxcpp/go-mockdns

srv, _ := mockdns.NewServer(map[string]mockdns.Zone{
    "example.org.": {
        A: []string{"1.2.3.4"},
    },
}, false)
defer srv.Close()

srv.PatchNet(net.DefaultResolver)
defer mockdns.UnpatchNet(net.DefaultResolver)

addrs, err := net.LookupHost("example.org")
fmt.Println(addrs, err)

it also has an option to create a mock resolver object but then you have to use dependency injection to inject the mock resolver to be used internally:

r := mockdns.Resolver{
    Zones: map[string]mockdns.Zone{
        "example.org.": {
            A: []string{"1.2.3.4"},
        },
    },
}

addrs, err := r.LookupHost(context.Background(), "example.org")
fmt.Println(addrs, err)

huangapple
  • 本文由 发表于 2014年7月4日 07:47:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/24564840.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定