使用 Elastic 进行 Golang 模拟

use*_*831 5 unit-testing go

我在 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
}
Run Code Online (Sandbox Code Playgroud)

而现在测试...

  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)
  }
Run Code Online (Sandbox Code Playgroud)

我因以下错误而屈服:cannot use m (type MockClient) as type *elastic.Client in argument to CheckBucketExists.

我假设这是我使用 elastic.client 类型的基础,但我仍然太菜鸟。

ces*_*egb 5

这是一个老问题,但也找不到解决方案。不幸的是,这个库是使用结构实现的,这使得模拟它一点也不简单,所以我找到的选项是:

(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)
}
Run Code Online (Sandbox Code Playgroud)

然后像模拟应用程序的其他模块一样模拟此界面和响应。

(2) 另一种选择就像这篇博文中指出的那样,它是使用 Rest 调用来模拟响应的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))
    }
}
Run Code Online (Sandbox Code Playgroud)

然后创建一个虚拟的 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
}
Run Code Online (Sandbox Code Playgroud)

在本例中,我有一个库来构建我的 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)
Run Code Online (Sandbox Code Playgroud)

其中,mockLibESClient 是我提到的库,我们对该mockLibESClient.GetEmployeeSearchServices方法进行存根处理,使其返回 SearchService,从而返回预期的有效负载。

注意:为了创建模拟mockLibESClient,我使用了https://github.com/golang/mock

我发现这很复杂,但在我看来,“包装”elastic.Client 需要做更多的工作。

问题:我尝试通过使用https://github.com/vburenin/ifacemaker创建接口来模拟它,然后使用它来模拟该接口并https://github.com/golang/mock使用它,但是当尝试返回接口而不是结构时,我不断收到兼容性错误,我不是 Go 期望无论如何,我可能需要更好地理解类型转换才能解决这个问题。因此,如果你们中有人知道如何做到这一点,请告诉我。


Int*_*net -1

线路

func CheckBucketExists(name string, client *elastic.Client) bool {
Run Code Online (Sandbox Code Playgroud)

表示CheckBucketExists期望 a *elastic.Client.

线路:

m := MockClient{}
m.On("IndexExists", "thisuri").Return(true) 
r := CheckBucketExists("thisuri", m)
Run Code Online (Sandbox Code Playgroud)

将 a 传递MockClientCheckBucketExists函数。

这导致了类型冲突。

也许您需要导入github.com/olivere/elastic到测试文件中并执行以下操作:

m := &elastic.Client{}
Run Code Online (Sandbox Code Playgroud)

代替

m := MockClient{}
Run Code Online (Sandbox Code Playgroud)

但我不是 100% 确定你想做什么。