gpt4 book ai didi

用 ginkgo 麻烦测试两个字符串

转载 作者:IT王子 更新时间:2023-10-29 01:36:54 26 4
gpt4 key购买 nike

首先,我想告诉你,我是 go 的新手,而且我来自 Python。话虽如此,我可以继续解决我的问题。我遇到了以下问题:

cacciald@cacciald-Lenovo-G470:~/workspace/gopath/src/github.com/lcacciagioni/bosh_web_console/executor$ go test
Running Suite: Executor Suite
=============================
Random Seed: 1409854483
Will run 1 of 1 specs

• Failure [0.005 seconds]
Executor
/home/cacciald/workspace/gopath/src/github.com/lcacciagioni/bosh_web_console/executor/executor_test.go:21
Should execute a command [It]
/home/cacciald/workspace/gopath/src/github.com/lcacciagioni/bosh_web_console/executor/executor_test.go:20

Expected
<string>: Hello World

to be equivalent to
<string>: Hello World

/home/cacciald/workspace/gopath/src/github.com/lcacciagioni/bosh_web_console/executor/executor_test.go:19
------------------------------

Summarizing 1 Failure:

[Fail] Executor [It] Should execute a command
/home/cacciald/workspace/gopath/src/github.com/lcacciagioni/bosh_web_console/executor/executor_test.go:19

Ran 1 of 1 Specs in 0.006 seconds
FAIL! -- 0 Passed | 1 Failed | 0 Pending | 0 Skipped --- FAIL: TestExecutor (0.01 seconds)
FAIL
exit status 1
FAIL github.com/lcacciagioni/bosh_web_console/executor 0.019s

代码如下:

// Package executor will provide a way to execute console commands in our
// Operative System.-
package executor

import (
"bytes"
"log"
"os/exec"
)

func Runner(command, params string) string {
cmd := exec.Command(command, params)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
return out.String()
}

这是我的简单测试:

package executor_test

import (
. "github.com/lcacciagioni/bosh_web_console/executor"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("Executor", func() {
var (
cmd string
params string
)

It("Should execute a command", func() {
cmd = "echo"
params = "Hello World"
Expect(Runner(cmd, params)).To(BeEquivalentTo(params))
})
})

如果有人能告诉我为什么这行不通那就太好了!!!

最佳答案

您忘记了 echo\n

这很好用:

 var _ = Describe("Executor", func() {
var (
cmd string
params string
)

It("Should execute a command", func() {
cmd = "echo"
params = "Hello World"
Expect(Runner(cmd, params)).To(BeEquivalentTo(params + "\n"))
})
})

关于用 ginkgo 麻烦测试两个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25673046/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com