我在 Go 中构建了一个快速简单的 API 来查询 ElasticSearch。现在我知道它可以完成,我想通过添加测试来正确地完成它。我已经抽象了一些我的代码,以便它可以进行单元测试,但是我在模拟弹性库时遇到了一些问题,因此我认为最好尝试一个简单的案例来模拟它。
import (
"encoding/json"
"github.com/olivere/elastic"
"net/http"
)
...
func CheckBucketExists(name string, client *elastic.Client) bool {
exists, err := client.IndexExists(name).Do()
if err != nil {
panic(err)
}
return exists
}
现在测试...
import (
"fmt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"testing"
)
type MockClient struct {
mock.Mock
}
func (m *MockClient) IndexExists(name string) (bool, error) {
args := m.Mock.Called()
fmt.Println("This is a thing")
return args.Bool(0), args.Error(1)
}
func TestMockBucketExists(t *testing.T) {
m := MockClient{}
m.On("IndexExists", "thisuri").Return(true)
>> r := CheckBucketExists("thisuri", m)
assert := assert.New(t)
assert.True(r, true)
}
我遇到了以下错误:不能在 CheckBucketExists 的参数中使用 m(类型 MockClient)作为类型 *elastic.Client。
我假设这是我使用 elastic.client 类型的基础,但我仍然是一个菜鸟。
最佳答案
这是一个老问题,但也找不到解决方案。 不幸的是,这个库是使用结构实现的,这使得模拟它一点也不简单,所以我找到的选项是:
(1) 将所有 elastic.SearchResult 方法包装在您自己的接口(interface)上并“代理”调用,因此您最终得到如下结果:
type ObjectsearchESClient interface {
// ... all methods...
Do(context.Context) (*elastic.SearchResult, error)
}
// NewObjectsearchESClient returns a new implementation of ObjectsearchESClient
func NewObjectsearchESClient(cluster *config.ESCluster) (ObjectsearchESClient, error) {
esClient, err := newESClient(cluster)
if err != nil {
return nil, err
}
newClient := objectsearchESClient{
Client: esClient,
}
return &newClient, nil
}
// ... all methods...
func (oc *objectsearchESClient) Do(ctx context.Context) (*elastic.SearchResult, error) {
return oc.searchService.Do(ctx)
}
然后像模拟应用程序的其他模块一样模拟此接口(interface)和响应。
(2) 另一个选项就像在 this blog post 中指出的那样这是使用 httptest.Server
为此,我模拟了处理程序,包括模拟来自“HTTP 调用”的响应
func mockHandler () http.HandlerFunc{
return func(w http.ResponseWriter, r *http.Request) {
resp := `{
"took": 73,
"timed_out": false,
... json ...
"hits": [... ]
...json ... ,
"aggregations": { ... }
}`
w.Write([]byte(resp))
}
}
然后你创建一个虚拟的 elastic.Client 结构体
func mockClient(url string) (*elastic.Client, error) {
client, err := elastic.NewSimpleClient(elastic.SetURL(url))
if err != nil {
return nil, err
}
return client, nil
}
在这种情况下,我有一个构建我的 elastic.SearchService 并返回它的库,所以我使用 HTTP 如下:
...
ts := httptest.NewServer(mockHandler())
defer ts.Close()
esClient, err := mockClient(ts.URL)
ss := elastic.NewSearchService(esClient)
mockLibESClient := es_mock.NewMockSearcherClient(mockCtrl)
mockLibESClient.EXPECT().GetEmployeeSearchServices(ctx).Return(ss, nil)
其中 mockLibESClient 是我提到的库,我们 stub mockLibESClient.GetEmployeeSearchServices 方法,使其返回 SearchService,它将返回预期的有效负载。
注意:为了创建模拟 mockLibESClient,我使用了 https://github.com/golang/mock
我发现这很复杂,但在我看来,“包裹”elastic.Client 需要更多工作。
问题:我尝试通过使用 https://github.com/vburenin/ifacemaker 创建接口(interface)来模拟它,然后使用 https://github 模拟该接口(interface)。 com/golang/mock 并有点使用它,但是在尝试返回接口(interface)而不是结构时我一直遇到兼容性错误,我根本不是 Go expect 所以可能我需要了解类型转换 a能够像那样解决它会更好一些。因此,如果你们中有人知道如何使用它,请告诉我。
关于unit-testing - Golang 模拟 Elastic,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28454337/
我正在尝试测试是否存在表单。我是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
是的,我知道最好使用webmock,但我想知道如何在RSpec中模拟此方法:defmethod_to_testurl=URI.parseurireq=Net::HTTP::Post.newurl.pathres=Net::HTTP.start(url.host,url.port)do|http|http.requestreq,foo:1endresend这是RSpec:let(:uri){'http://example.com'}specify'HTTPcall'dohttp=mock:httpNet::HTTP.stub!(:start).and_yieldhttphttp.shou
假设我在Store的模型中有这个非常简单的方法:defgeocode_addressloc=Store.geocode(address)self.lat=loc.latself.lng=loc.lngend如果我想编写一些不受地理编码服务影响的测试脚本,这些脚本可能已关闭、有限制或取决于我的互联网连接,我该如何模拟地理编码服务?如果我可以将地理编码对象传递到该方法中,那将很容易,但我不知道在这种情况下该怎么做。谢谢!特里斯坦 最佳答案 使用内置模拟和stub的rspecs,你可以做这样的事情:setupdo@subject=MyCl
在ruby中,你可以这样做:classThingpublicdeff1puts"f1"endprivatedeff2puts"f2"endpublicdeff3puts"f3"endprivatedeff4puts"f4"endend现在f1和f3是公共(public)的,f2和f4是私有(private)的。内部发生了什么,允许您调用一个类方法,然后更改方法定义?我怎样才能实现相同的功能(表面上是创建我自己的java之类的注释)例如...classThingfundeff1puts"hey"endnotfundeff2puts"hey"endendfun和notfun将更改以下函数定
我有一个gem,它有一个根据Rails.env的不同行为的方法:defself.envifdefined?(Rails)Rails.envelsif...现在我想编写一个规范来测试这个代码路径。目前我是这样做的:Kernel.const_set(:Rails,nil)Rails.should_receive(:env).and_return('production')...没关系,只是感觉很丑。另一种方法是在spec_helper中声明:moduleRails;end而且效果也很好。但也许有更好的方法?理想情况下,这应该有效:rails=double('Rails')rails.sho
在Test::Unit中的ruby单元测试断言失败后,在执行teardown之前,是否有一些简洁优雅的方法来立即执行我的代码?我正在做一些自动化的GUI测试,并希望在出现问题后立即截图。 最佳答案 如果您使用的是1.9,请不要使用Test::Unit::TestCase作为您的基类。对其进行子类化并覆盖#run_test以进行救援,截取屏幕截图并重新提出:classMyAbstractTestCase或者,我认为这实际上是最简洁的方法,您可以使用before_teardownHook:classMyTestCase这不适用于1.
如何在ELB上设置和更新ruby版本?我已经在我们的qa和暂存环境中使用ruby2.2.2大约8个月了。我刚刚在星期一设置我们的生产环境,它不会部署,因为它说ruby设置为2.2.3而我的gemfile说2.2.2。我更新并重新部署,一切似乎都很好。我回到了qa/staging环境,但无法将其更新到ruby2.2.3。一直说ruby版本是2.2.2,Gemfile是2.2.3我升级了(通过elbui):运行Ruby2.2(PassengerStandalone)的64位AmazonLinux2015.03v1.3.1到运行Ruby2.2(PassengerStandalon
我有一个rspec模拟对象,一个值赋给了属性。我正在努力在我的rspec测试中满足这种期望。只是想知道语法是什么?代码:defcreate@new_campaign=AdCampaign.new(params[:new_campaign])@new_campaign.creationDate="#{Time.now.year}/#{Time.now.mon}/#{Time.now.day}"if@new_campaign.saveflash[:status]="Success"elseflash[:status]="Failed"endend测试it"shouldabletocreat
我正在尝试测试命令行工具的输出。如何使用rspec来“伪造”命令行调用?执行以下操作不起作用:it"shouldcallthecommandlineandreturn'text'"do@p=Pig.new@p.should_receive(:run).with('my_command_line_tool_call').and_return('resulttext')end如何创建stub? 最佳答案 使用newmessageexpectationsyntax:规范/虚拟规范.rbrequire"dummy"describeDummy
显然在Test::Unit中没有assert_false。您将如何通过扩展断言并添加文件config/initializers/assertions_helper.rb来添加它?这是最好的方法吗?我不想修改test/unit/assertions.rb。顺便说一句,我不认为这是多余的。我使用的是assert_equalfalse,something_to_evaluate。这种方法的问题是很容易意外使用assertfalse,something_to_evaluate。这将始终失败,不会引发错误或警告,并且会在测试中引入错误。 最佳答案