fab*_*ous 3 testing unit-testing go
我有以下文件树结构:
-app/
---tool/
-----/tool_test.go
-----/tool.go
-----/proto/proto.go
-----/proto/proto_test.go
Run Code Online (Sandbox Code Playgroud)
我需要使用一个(虚拟)结构来实现tool_test.go和中的接口proto_test.go:
type DummyRetriever struct{}
func (dummy *DummyRetriever) Retrieve(name string) (string, error) {
return "", nil
}
Run Code Online (Sandbox Code Playgroud)
如果我tool_test.go仅在 中定义它,则无法在 中看到和使用它proto_test.go,因为 _test.go 文件不导出名称。
我在哪里定义DummyRetriever以便它在两个包中都可用?我想避免在文件中定义它,以便该名称在核心(非测试)包中也可见。
如果您需要两个不同包中的模拟,则模拟不能存在于测试文件(以 结尾的文件_test.go)中。
如果您不关心在哪里使用模拟,那么只需创建一个mock包并将其放在那里即可。
-app/
---tool/
-----mock/
-------/dummyretriever.go
-------/othermock.go
-----/tool_test.go
-----/tool.go
-----/proto/proto.go
-----/proto/proto_test.go
Run Code Online (Sandbox Code Playgroud)
如果您只想从该包或其后代中使用模拟,则将其放入该internal包中。
-app/
---tool/
-----internal/
-------/dummyretriever.go
-------/othermock.go
-----/tool_test.go
-----/tool.go
-----/proto/proto.go
-----/proto/proto_test.go
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1286 次 |
| 最近记录: |