我真的是Golang的新手,我对测试有疑问。
我有一个测试,我想检查客户是否在Elasticsearch中坚持不懈。我已将代码缩减至关键部分,并将其发布在github上:(https://github.com/fvosberg/elastic-go-testing)

问题是,我必须等待elasticsearch为新文档建立索引,然后才能进行搜索。除了等待一秒钟,还有其他选择吗?这感觉很丑陋,但是我不知道如何以另一种方式测试集成(使用Elasticsearch并用小写的电子邮件地址...)。

有解决这个问题的方法吗?

package main

import (
    "github.com/fvosberg/elastic-go-testing/customer"
    "testing"
    "time"
)

func TestRegistration(t *testing.T) {
    testCustomer := customer.Customer{Email: "testing@test.de"}
    testCustomer.Create()
    time.Sleep(time.Second * 1)
    _, err := customer.FindByEmail("testing@test.de")
    if err != nil {
        t.Logf("Error occured: %+v\n", err)
        t.Fail()
    } else {
        t.Log("Found customer testing@test.de")
    }
}

最佳答案

Elasticsearch有一个flush命令,在这种情况下很有用。由于您将elastic项目用作接口(interface),因此可以使用以下命令(其中client是您的ES客户端):
...testCustomer.Create()res, err := client.Flush().Do() if err != nil { t.Fatal(err) }_, err := customer.FindByEmail("testing@test.de")...

08-07 21:54
查看更多