我很新,正在尝试为 go 编写一个测试文件。当我运行测试时,我有两个问题: 1. 我必须运行“go test -cover”,然后我才能看到这个:
#command-line-arguments
./client_test.go:59: undefined: Init
FAIL command-line-arguments [build failed]
我的问题是为什么我不能只运行:去测试(如果我这样做,我会看到这个:
exit status 1
FAIL command-line-arguments 0.008s
这几天我一直在网上查看解决方案。任何想法都会有所帮助。 很抱歉,我无法显示代码,非常感谢您的帮助。
谢谢
编辑2: 谢谢,伙计们,我正在考虑环境变量,(我正在设置它们)。我真的很感谢你的意见。
编辑1: 感谢您的回复,我确实尝试了一小段代码来查看 go test 是否正常: (对不起,我不能做我正在做的项目) 你好.go:
package main
func hello1(i int) string {
if i > 0 {
return "Hello, world"
}
return "no value"
}
hello_test.go:
package main
import (
"testing"
"fmt"
)
func TestHello(t *testing.T) {
var s string
var s2 string
var s3 string
s = hello1(3)
if s != "Hello, world" {
t.Error("Expected: Hello, world, got", s)
}
s2 = hello1(-3)
if s2 != "Hello, world" {
t.Error("Expected: Hello, world, got", s2)
}
s3 = hello1(0)
if s3 != "Hello, world" {
t.Error("Expected: Hello, world, got", s3)
}
}
当我使用 IDE 运行这些文件时,我看到:
GOROOT=/usr/local/Cellar/go/1.9.3/libexec #gosetup
GOPATH=/Users/<here is my user name>/go #gosetup
/usr/local/Cellar/go/1.9.3/libexec/bin/go test -c -i -o /private/var/folders/cx/0mvyxrxj5755jc68mqvtywth0000gn/T/___TestHello_in_hello_test_go /Users/minghan/awesomeProject/hello_test.go #gosetup
# command-line-arguments
./hello_test.go:13:6: undefined: hello1
./hello_test.go:17:7: undefined: hello1
./hello_test.go:21:7: undefined: hello1
Compilation finished with exit code 2
当我在命令行中运行时:go test -cover:
--- FAIL: TestHello (0.00s)
hello_test.go:19: Expected: Hello, world, got no value
hello_test.go:23: Expected: Hello, world, got no value
FAIL
coverage: 100.0% of statements
exit status 1
FAIL _/Users/minghan/awesomeProject 0.007s
最佳答案
对于写go来说,拥有正确的文件夹结构真的很重要,这样你以后就不会头疼了。你需要这样的 gopath:
bin/
hello # command executable
outyet # command executable
pkg/
linux_amd64/
github.com/golang/example/
stringutil.a # package object
src/
github.com/golang/example/
.git/ # Git repository metadata
hello/
hello.go # command source
outyet/
main.go # command source
main_test.go # test source
stringutil/
reverse.go # package source
reverse_test.go # test source
golang.org/x/image/
.git/ # Git repository metadata
bmp/
reader.go # package source
writer.go # package source
... (many more repositories and packages omitted) ...
(取自https://golang.org/doc/code.html#Workspaces)
默认情况下,gopath 是“$HOME/go”或其他操作系统上的等效路径 (home_directory/go)
所以你最终会在这样的地方得到你的项目:$HOME/go/src/github.com/matthin/awesomeproject
并将所有文件放入其中。
然后要运行测试,您可以执行 go test github.com/matthin/awesomeproject 或者您可以通过指定该目录中的每个文件来运行测试,就像您的情况一样:
去测试main.go main_test.go
然后 go test runner 知道它需要评估两个文件并且会找到函数。
关于testing - Golang 测试,相同的包,未定义的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48775370/
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
我正在编写一个包含C扩展的gem。通常当我写一个gem时,我会遵循TDD的过程,我会写一个失败的规范,然后处理代码直到它通过,等等......在“ext/mygem/mygem.c”中我的C扩展和在gemspec的“扩展”中配置的有效extconf.rb,如何运行我的规范并仍然加载我的C扩展?当我更改C代码时,我需要采取哪些步骤来重新编译代码?这可能是个愚蠢的问题,但是从我的gem的开发源代码树中输入“bundleinstall”不会构建任何native扩展。当我手动运行rubyext/mygem/extconf.rb时,我确实得到了一个Makefile(在整个项目的根目录中),然后当
我有一个围绕一些对象的包装类,我想将这些对象用作散列中的键。包装对象和解包装对象应映射到相同的键。一个简单的例子是这样的:classAattr_reader:xdefinitialize(inner)@inner=innerenddefx;@inner.x;enddef==(other)@inner.x==other.xendenda=A.new(o)#oisjustanyobjectthatallowso.xb=A.new(o)h={a=>5}ph[a]#5ph[b]#nil,shouldbe5ph[o]#nil,shouldbe5我试过==、===、eq?并散列所有无济于事。
我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re