在这里,我正在尝试在go命令行应用程序上进行BDD.我正在使用Ginkgo,它包装了testing.go并让你做更具表现力的BDD.https://github.com/onsi/ginkgo
我在阅读stdout以解决问题时遇到了问题.
发现在pkg/testing示例中运行之前存根输出但我无法找到读取该输出的方法:http://golang.org/src/pkg/testing/example.go
这就是我想做的事情:
package cli
import "fmt"
func Run() {
fmt.Println("Running cli")
}
Run Code Online (Sandbox Code Playgroud)
package cli_test
import (
. "github.com/altoros/bosh_deployer_cli/lib/cli"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Cli", func() {
It("should parse update stemcell flag", func() {
Run()
Expect(stdout).To(Equal("running cli"))
})
})
Run Code Online (Sandbox Code Playgroud)
测试Stdout可能很棘手.你有多种选择.
您可以在测试期间覆盖os.Stdout :(想想检查错误)
var _ = Describe("Cli", func() {
It("should parse update stemcell flag", func() {
r, w, _ := os.Pipe()
tmp := os.Stdout
defer func() {
os.Stdout = tmp
}()
os.Stdout = w
go func() {
Run()
w.Close()
}()
stdout, _ := ioutil.ReadAll(r)
Expect(string(stdout)).To(Equal("Running cli\n"))
})
})
Run Code Online (Sandbox Code Playgroud)
或者您可以将作者传递给您的函数:
package cli
import (
"fmt"
"io"
)
func Run(w io.Writer) {
fmt.Fprintln(w, "Running cli")
}
Run Code Online (Sandbox Code Playgroud)
package cli_test
import (
. "cli"
"io"
"io/ioutil"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Cli", func() {
It("should parse update stemcell flag", func() {
r, w := io.Pipe()
go func() {
Run(w)
w.Close()
}()
stdout, _ := ioutil.ReadAll(r)
Expect(string(stdout)).To(Equal("Running cli\n"))
})
})
Run Code Online (Sandbox Code Playgroud)
package main
import (
"cli"
"os"
)
func main() {
cli.Run(os.Stdout)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2997 次 |
| 最近记录: |