mardi 18 août 2015

Mocking an ElasticSearch client using C# Moq

I'm testing my class ElasticUtility which requires an instance of the ElasticClient in order to work properly so I mocked such class and injected it into the ElasticUtility instance (utility)

    private ElasticUtility utility;
    private Mock<IElasticClient> elasticClientMock;
    private string elasticSearchIndexName;

    elasticClientMock = new Mock<IElasticClient>();
    utility = new UhhElasticUtility(elasticClientMock.Object);

This is the actual test code:

[Test]
public void GetGetPvDataClientReturnNull()
{
    // arrange
    var groupId = "groupid";
    var startTime = new DateTime(2015, 08, 17, 13, 30, 00);
    var endTime = new DateTime(2015, 08, 17, 13, 40, 00);

    // act
    utility.GetPvData(groupId, startTime, endTime);

    // assert
    elasticClientMock.Verify(ec => ec.Search<SegmentRecord>(It.IsAny<Nest.ISearchRequest>()), Times.Once());
}

I get a Null reference exception when the Moq library calls the .Search() method inside the mocked ElastiClient.

Aucun commentaire:

Enregistrer un commentaire