doudang1052 2017-03-15 22:37
浏览 41
已采纳

仅为_test.go文件定义结构

I have the following tree structure of files:

-app/
---tool/
-----/tool_test.go
-----/tool.go
-----/proto/proto.go
-----/proto/proto_test.go

I need to use a (dummy) struct implementing an interface in both tool_test.go and proto_test.go:

type DummyRetriever struct{}

func (dummy *DummyRetriever) Retrieve(name string) (string, error) {
  return "", nil 
}

If I define it in tool_test.go only, I can't see and use it in proto_test.go, as _test.go files don't export names.

Where do I define the DummyRetriever so that it is available in both packages? I want to avoid having it to define in a file so that the name is then also visible in core (non-test) packages.

  • 写回答

2条回答 默认 最新

  • dsgdg54ef4365 2017-03-15 22:51
    关注

    If you need the mock in two different packages, the mock can't exist in a test file (a file ending in _test.go).

    If you don't care where the mocks are used, then just create a mock package and put there.

    -app/
    ---tool/
    -----mock/
    -------/dummyretriever.go
    -------/othermock.go
    -----/tool_test.go
    -----/tool.go
    -----/proto/proto.go
    -----/proto/proto_test.go
    

    If you only want the mocks to be used from that package or its descendants, then put it in the internal package.

    -app/
    ---tool/
    -----internal/
    -------/dummyretriever.go
    -------/othermock.go
    -----/tool_test.go
    -----/tool.go
    -----/proto/proto.go
    -----/proto/proto_test.go
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料