# Using InMemoryConnection to test ElasticSearch

**URL:** <https://discuss.elastic.co/t/using-inmemoryconnection-to-test-elasticsearch/54750>\
**Category:** Elasticsearch\
**Created:** [July 5, 2016, 2:44pm UTC](https://discuss.elastic.co/t/using-inmemoryconnection-to-test-elasticsearch/54750 "2016-07-05T14:44:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_D\_Ambra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paul_d_ambra/32/10728_2.png) [@Paul\_D\_Ambra](https://discuss.elastic.co/u/Paul_D_Ambra)\
**Post date:** [July 5, 2016, 2:44pm UTC](https://discuss.elastic.co/t/using-inmemoryconnection-to-test-elasticsearch/54750/1 "2016-07-05T14:44:17Z")

</div>

I'm trying to add testing around our use of ElasticSearch (in C# using Nest 1.4.2) and want to use InMemoryConnection but I'm missing something (I assume) and having no success.

I've created this simple Nunit test case as a boiled down example of my issue

`  
using System;  
using Elasticsearch.Net.Connection;  
using FluentAssertions;  
using Nest;  
using NUnit.Framework;

```
namespace NestTest
{
    public class InMemoryConnections
    {
        public class TestThing
        {
            public string Stuff { get; }

            public TestThing(string stuff)
            {
                Stuff = stuff;
            }
        }

        [Test]
        public void CanBeQueried()
        {
            var connectionSettings = new ConnectionSettings(new Uri("http://foo.test"), "default_index");

            var c = new ElasticClient(connectionSettings, new InMemoryConnection());
            c.Index(new TestThing("peter rabbit"));

            var result = c.Search<TestThing>(sd => sd);

            result.ConnectionStatus.Success.Should().BeTrue();
        }
    }
}

```

`

The connection status reports false for success, an http status code of 0, and an original exception of "{"Could not parse server exception"}"

---

<div class="post-metadata">

**Author:** ![Paul\_D\_Ambra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paul_d_ambra/32/10728_2.png) [@Paul\_D\_Ambra](https://discuss.elastic.co/u/Paul_D_Ambra)\
**Post date:** [July 5, 2016, 2:55pm UTC](https://discuss.elastic.co/t/using-inmemoryconnection-to-test-elasticsearch/54750/2 "2016-07-05T14:55:49Z")

</div>

Ah! If I create the elastic client using

`var c = new ElasticClient(connectionSettings, new InMemoryConnection(connectionSettings));`

the query is now successful but doesn't find the document I just indexed...

---

<div class="post-metadata">

**Author:** ![Paul\_D\_Ambra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paul_d_ambra/32/10728_2.png) [@Paul\_D\_Ambra](https://discuss.elastic.co/u/Paul_D_Ambra)\
**Post date:** [July 5, 2016, 3:16pm UTC](https://discuss.elastic.co/t/using-inmemoryconnection-to-test-elasticsearch/54750/3 "2016-07-05T15:16:24Z")

</div>

Updating to 2.3.3 and new syntax

```auto
        [Test]
        public void CanBeQueried()
        {

            var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
            var settings = new ConnectionSettings(connectionPool, new InMemoryConnection());
            settings.DefaultIndex("default");

            var c = new ElasticClient(settings);
            
            c.Index(new TestThing("peter rabbit"));

            var result = c.Search<TestThing>(sd => sd);
            
            result.CallDetails.Success.Should().BeTrue();
            result.Documents.Single().Stuff.Should().Be("peter rabbit");
        }

```

fails in the same way... i.e. the query is reported as successful but returns 0 documents

---

<div class="post-metadata">

**Author:** ![Paul\_D\_Ambra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paul_d_ambra/32/10728_2.png) [@Paul\_D\_Ambra](https://discuss.elastic.co/u/Paul_D_Ambra)\
**Post date:** [July 5, 2016, 3:20pm UTC](https://discuss.elastic.co/t/using-inmemoryconnection-to-test-elasticsearch/54750/4 "2016-07-05T15:20:29Z")

</div>

I've crossposted here [http://stackoverflow.com/questions/38207159/using-inmemoryconnection-to-test-elasticsearch](http://stackoverflow.com/questions/38207159/using-inmemoryconnection-to-test-elasticsearch)

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [July 6, 2016, 5:33am UTC](https://discuss.elastic.co/t/using-inmemoryconnection-to-test-elasticsearch/54750/5 "2016-07-06T05:33:57Z")

</div>

> the query is now successful but doesn't find the document I just indexed...

That's most likely because the index hasn't been refreshed since the document was indexed. You should be able to request a refresh as part of your index request.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 10:37pm UTC](https://discuss.elastic.co/t/using-inmemoryconnection-to-test-elasticsearch/54750/6 "2017-07-05T22:37:42Z")

</div>


