我的帖子有2个问题
我本质上是想了解如何将 gomock 与 gingko 一起使用
路径
/Users/Ratatouille/Desktop/test/goExample
以下项目位于
/Users/Ratatouille/Desktop/test/goExample/square
具有以下项目结构
.
├── area.go
└── area_test
├── area_mock.go
├── area_mock_test.go
└── area_suite_test.go
我无法理解为什么会出现以下错误
Failed to compile area_test:
can't load package: package ./area_test: found packages area_test (area_mock.go) and area (area_mock_test.go) in /Users/Ratatouille/Desktop/test/goExample/square/area_test
Ginkgo ran 1 suite in 303.243871ms
Test Suite Failed
我的第二个问题是项目 repo 有 area.go 文件,看起来像这样
package main
import "fmt"
type Square struct {
side float32
}
func (s *Square) Area() float32 {
return s.side * s.side
}
func (s *Square) CalculateArea(shaper Shaper) float32 {
shaper.Area()
}
type Shaper interface {
Area() float32
}
func main() {
sq1 := new(Square)
sq1.side = 2
var areaIntf Shaper
areaIntf = sq1
fmt.Println(areaIntf.Area())
}
我的问题是如何在 area_mock_test.go 中导入 main 包
最佳答案
您的项目结构不是 Go 所期望的。
area_test/ 不是area 的测试。我猜你运行了 go test ./area_test。这告诉 Go ./area_test 是一个项目。它尝试编译 area_mock.go 并尝试使用 area_mock_test.go 和 area_suite_test.go 作为其测试。你得到的错误是 Go 笨拙的方式告诉你它不希望在项目文件中找到名为 *_test 的包。
area.go 也是一个问题。您使用 package main 获得它,然后尝试使用 package area_test 进行测试。他们不匹配,Go 不会喜欢它。包目录包含一个包。它的测试必须使用该包或 thatpackage_test。这强制包目录只做一件事。它要么是要导入的库,要么是要运行的程序。两者都不是。
另外项目目录名为square,但包名为area。有与项目名称不匹配的文件是可以的,一个包中有多个文件也可以,但是使用一个包而不是项目目录名称是不好的做法。
还有一个问题。 Go 希望源文件位于 $GOPATH/src 中。您可以直接在 $GOPATH 中获取它们。导入语句找不到它们。
Go 的项目结构可能需要一些时间来适应,它对项目和包的结构有非常坚定的想法(Go 对如何编写代码以及如何不编写代码有非常坚定的想法).不要打它。帮自己一个忙,使用 ~/go 的默认 GOPATH 并将你的代码放在 ~/go/src/ 中。
您可以像这样编写方形库。
~/go/src/square/
|--- square.go
|--- square_test.go
square.go 可能看起来像这样。
package square
type Square struct {
Side float32
}
func (s Square) Area() float32 {
return s.Side * s.Side
}
请注意,Side 必须大写才能成为公共(public)数据成员。另请注意,它是按值传递的,在 Go 中,如果您打算修改结构,则样式仅按指针传递。
square_test.go 可能看起来像这样。
package square_test
import(
"testing"
"square"
"github.com/stvp/assert"
)
func TestArea( t *testing.T ) {
sq := square.Square{Side: 5}
assert.Equal( t, sq.Area(), float32(25) )
}
Go 没有任何断言。 github.com/stvp/assert提供基础知识并消除很多单调乏味。您可以通过 go get github.com/stvp/assert 获取它。
请注意,在package square_test 中使此成为黑盒测试,只能使用square 的公共(public)接口(interface)。如果你想要一个玻璃盒/内部测试,它会使用 package square 并进入它自己的文件,如 square_internal_test.go。
如果你想要一个使用 square 的程序,那将在它自己单独的包目录中。它将有一个带有 package main 和 import "square" 的 main.go。
How To Write Go对此进行了更详细的解释。
关于go - Golang 上的包导入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44883077/
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问
我将我的Rails应用程序部署到OpenShift,它运行良好,但我无法在生产服务器上运行“Rails控制台”。它给了我这个错误。我该如何解决这个问题?我尝试更新rubygems,但它也给出了权限被拒绝的错误,我也无法做到。railsc错误:Warning:You'reusingRubygems1.8.24withSpring.UpgradetoatleastRubygems2.1.0andrun`gempristine--all`forbetterstartupperformance./opt/rh/ruby193/root/usr/share/rubygems/rubygems
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
我克隆了一个rails仓库,我现在正尝试捆绑安装背景:OSXElCapitanruby2.2.3p173(2015-08-18修订版51636)[x86_64-darwin15]rails-v在您的Gemfile中列出的或native可用的任何gem源中找不到gem'pg(>=0)ruby'。运行bundleinstall以安装缺少的gem。bundleinstallFetchinggemmetadatafromhttps://rubygems.org/............Fetchingversionmetadatafromhttps://rubygems.org/...Fe
在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee
我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie