我已经在Go中构建了一个快速而简单的应用程序接口来查询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)
}
我得到了如下的错误:cannot use m (type MockClient) as type *elastic.Client in argument to CheckBucketExists
。
我假设这是我使用elastic.client类型的基础,但我仍然是个新手。
发布于 2018-01-21 06:22:33
这是一个古老的问题,但也找不到解决方案。不幸的是,这个库是使用struct实现的,这使得模拟它一点也不简单,所以我找到的选项是:
(1)自己包装接口上的所有elastic.SearchResult
方法,并“代理”调用,因此您最终得到的结果如下所示:
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)
}
然后模拟此界面和响应,就像您对应用程序的其他模块所做的那样。
(2)另一种选择类似于this blog post中指出的,即使用httptest.Server
模拟来自Rest调用的响应
为此,我模拟了处理程序,其中包括模拟来自"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并返回它的库,所以我使用如下的超文本传输协议:
...
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是我提到的库,我们对mockLibESClient.GetEmployeeSearchServices
方法进行存根,使其返回将返回预期有效负载的SearchService。
注意:为了创建模拟mockLibESClient,我使用了https://github.com/golang/mock
我发现这很复杂,但在我看来,“包装”elastic.Client是更多的工作。
问:我试图通过使用https://github.com/vburenin/ifacemaker
创建一个接口来模拟它,然后用https://github.com/golang/mock
模拟那个接口并使用它,但是当我试图返回一个接口而不是一个结构时,我一直收到兼容性错误,我根本不是一个Go expect,所以可能我需要更好地理解类型转换才能像那样解决它。所以,如果你们中的任何人知道怎么做,请让我知道。
发布于 2015-02-12 17:39:52
这条线
func CheckBucketExists(name string, client *elastic.Client) bool {
声明CheckBucketExists
需要一个*elastic.Client
。
这几行代码:
m := MockClient{}
m.On("IndexExists", "thisuri").Return(true)
r := CheckBucketExists("thisuri", m)
向CheckBucketExists
函数传递一个MockClient
。
这将导致类型冲突。
也许您需要将github.com/olivere/elastic
导入到您的测试文件中,然后执行以下操作:
m := &elastic.Client{}
而不是
m := MockClient{}
但我不是百分之百确定你想做什么。
https://stackoverflow.com/questions/28454337
复制相似问题