我刚刚学习了使用exec.Command()ie, mocking 的单元测试函数exec.Command()。我继续添加更多单元案例,但遇到了无法模拟不同场景输出的问题。
这是hello.go我正在尝试测试的示例代码...
package main
import (
"fmt"
"os/exec"
)
var execCommand = exec.Command
func printDate() ([]byte, error) {
cmd := execCommand("date")
out, err := cmd.CombinedOutput()
return out, err
}
func main() {
fmt.Printf("hello, world\n")
fmt.Println(printDate())
}
Run Code Online (Sandbox Code Playgroud)
下面是测试代码hello_test.go...
package main
import (
"fmt"
"os"
"os/exec"
"testing"
)
var mockedExitStatus = 1
var mockedDate = "Sun Aug 20"
var expDate = "Sun Aug 20"
func fakeExecCommand(command string, args ...string) *exec.Cmd {
cs …Run Code Online (Sandbox Code Playgroud)