还需要记住的一件事是,没有什么可以阻止您编写避免编写样板代码的函数。
例如,我有一个使用的命令行应用程序,
log我编写了以下函数:
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("removed certificate www.example.comn", output)使用此断言库:http :
//godoc.org/github.com/stretchr/testify/assert。



