更新时间:2023-11-08 21:26:28
还要记住一件事,没有什么能阻止您编写函数来避免样板文件.
One thing to also remember, there's nothing stopping you from writing functions to avoid the boilerplate.
例如,我有一个使用 log
的命令行应用程序,我编写了这个函数:
For example I have a command line app that uses log
and I wrote this function:
func captureOutput(f func()) string {
var buf bytes.Buffer
log.SetOutput(&buf)
f()
log.SetOutput(os.Stderr)
return buf.String()
}
然后像这样使用它:
output := captureOutput(func() {
client.RemoveCertificate("www.example.com")
})
assert.Equal(t, "removed certificate www.example.com
", output)
使用这个断言库:http://godoc.org/github.com/stretchr/testify/assert.