【发布时间】:2021-05-17 21:04:34
【问题描述】:
我的代码使用一个功能的接口:
func InsertRow(rec []string) error
这个接口的实现有不同的类型。现在我想用“go test”来测试它。在这种情况下, InsertRow 的实现应该什么都不做:
func (t TestInserter) InsertRow(rec []string) error {
return nil
}
我可以在测试函数中定义一个内部类型。但现在我也想为这种类型定义一个虚拟方法:
func TestInserter01(t *testing.T) {
type TestMyInserter struct {} <-- Test type
func (t TestMyInserter) InsertRow(rec []string) error { <-- Dummy function on this type.
return nil
}
... using InsertRow as a parameter in another function ...
}
但这会产生编译错误:
expected operand, found ']'
expected ']', found 'return'
如果我在测试函数之外定义类型和方法,则相同的代码可以工作。 是否可以在测试函数中隐藏测试实现而不在函数外部定义它?我需要其中很多,所以我更愿意在测试函数中本地定义它们。
【问题讨论】:
标签: go testing types interface local