mardi 10 mars 2020

How to unit test go code that interacts with Elasticsearch

So I have an application that defines a type Client struct {} which talks to various other clients in my code that talk to services like github, elasticsearch etc.

Now I have the following ES code in one of my packages



type SinkService interface {
    Write(context, index, mapping, doc)
}

type ESSink struct {
   client *elastic.Client
}

func NewESSink() *ESSink {}

 // checks if the index exists and writes the doc
func (s *ESSink) Write(context, index, mapping, doc) {}

I use this method in my main client that runs the whole application like this c.es.Write(...). Now if I want to write client_test.go I can simply make a mockESSink and use it with some stub code but that won't cover the lines written in my ES code

how do I unit test my ES code? my ESSink uses an elastic.Client how do I mock that?

I would like to embed some mock ES client that gives me stub responses and I will be able to test my ESSink.Write method that way.

Aucun commentaire:

Enregistrer un commentaire