Nest Connection : Mock 2 in-memory response in elastic search in c#


(Piyush Kumar) #1

I have to unit test a function that have 2 elastic search query and need to mock 2 responses for it. I am able to mock single response using below code .

var responseBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(JsonObject));
        var connection = new InMemoryConnection(responseBytes, 200);
        var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
        var settings = new ConnectionSettings(connectionPool, connection).DefaultIndex("TestElasticProject");
        ElasticSearchService.GetInstance().Init(settings);

I have both the mock responses but not to able to configure it in ConnectingSettings .

I need something like

var responseBytes1 = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(JsonObject));
    var connection1 = new InMemoryConnection(responseBytes1, 200);

var responseBytes2 = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(JsonObject));
var connection2 = new InMemoryConnection(responseBytes2, 200);

I have to unit test a function that have 2 elastic search query and need to mock 2 responses for it. I am able to mock single response using below code .

var responseBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(JsonObject));
            var connection = new InMemoryConnection(responseBytes, 200);
            var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
            var settings = new ConnectionSettings(connectionPool, connection).DefaultIndex("TestElasticProject");
            ElasticSearchService.GetInstance().Init(settings);

I have both the mock responses but not to able to configure it in ConnectingSettings .

I need something like

var responseBytes1 = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(JsonObject));
        var connection1 = new InMemoryConnection(responseBytes1, 200);
var responseBytes2 = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(JsonObject));
        var connection2 = new InMemoryConnection(responseBytes2, 200);

Is it possible in elastic search to assign 2 responseByte together ? because in ElasticSearch-Net code in GITHUB i am not able to find any implementation .

In Case its not possible then should i create 1 elastic search query per 1 function so that unit testing will be possible ?