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

报告相同问题?

悬赏问题

  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境