测试两个字符串与银杏麻烦

huangapple go评论144阅读模式
英文:

Testing two strings with ginkgo trouble

问题

首先,我想告诉你,我对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))
        })
})

如果有人能告诉我为什么这不起作用,那就太好了!

英文:

First of all I want to tell you that I’m very new at go and that I came from Python. Having saying that than I can continue with my problem. I’m having the following trouble:

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
      &lt;string&gt;: Hello World

  to be equivalent to
      &lt;string&gt;: 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

Here is the code:

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

import (
        &quot;bytes&quot;
        &quot;log&quot;
        &quot;os/exec&quot;
)

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

And here is my simple test:

package executor_test

import (
        . &quot;github.com/lcacciagioni/bosh_web_console/executor&quot;

        . &quot;github.com/onsi/ginkgo&quot;
        . &quot;github.com/onsi/gomega&quot;
)

var _ = Describe(&quot;Executor&quot;, func() {
        var (
                cmd    string
                params string
        )

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

If some can tell me why this is not working that will be great!!!

答案1

得分: 5

你忘记了echo\n

这个是正确的写法:

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

             It("应该执行一个命令", func() {
                     cmd = "echo"
                     params = "Hello World"
                     Expect(Runner(cmd, params)).To(BeEquivalentTo(params + "\n"))
             })
     })
英文:

You forgot the \n of echo.

this works fine:

 var _ = Describe(&quot;Executor&quot;, func() {
             var (
                     cmd    string
                     params string
             )

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

huangapple
  • 本文由 发表于 2014年9月5日 02:53:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/25673046.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定