9

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.

EDIT:

the constructor of ElasticUtility:

    protected ElasticUtility(IElasticClient elasticClient, string elasticIndexName)
    {
        this.ElasticClient = elasticClient;
        this.ElasticIndexName = elasticIndexName;
    }

EDIT: GetPvData() method:

    public IEnumerable<dynamic> GetPvData(string groupId, DateTime startTime, DateTime endTime)
    {
        var res = ElasticClient.Search<SegmentRecord>(s => s
            .Index(ElasticIndexName)
            .Filter(f =>
                f.Term(t => t.HistoryId, groupId) &&
                f.Range(i =>
                    i.OnField(a => a.DateTime).LowerOrEquals(startTime))).SortAscending(p => p.DateTime).Size(1)).Documents.ToList();

        return res.ToArray();
    }
8
  • please add the C'tor of UhhElasticUtility and the implementation of GetPvData Commented Aug 18, 2015 at 14:28
  • could it be because it's protected? Commented Aug 18, 2015 at 14:31
  • no protected C'tor is not the issue... but i asked about UhhElasticUtility C'tor and GetPvData... Commented Aug 18, 2015 at 14:34
  • and this is the GetPvData() implementation Commented Aug 18, 2015 at 14:36
  • the problem is .Documents you didn't set an expectation, then search return null... Commented Aug 18, 2015 at 14:38

2 Answers 2

6

The NullReferenceException occured because you didn't specify a behavior on search method. Your search method returns null and then you calls .Document on the null.

The way to specify a behavior is as the following:

elasticClientMock.Setup(x => x.Search<SegmentRecord>(
                             It.IsAny</* put here the right Func */>))
        .Returns( /* put here the instance you want to return */);

you have to replace my comments with the correct types.

Sign up to request clarification or add additional context in comments.

5 Comments

I'm unable to create an instance of Nest.ISearchResponse<SegmentRecord> to pass it to the .Returns() method.
if I write: var test = Nest.ISearchResponse<SegmentRecord>(); it says "Interface name is not valid at this point" . oh it's an interface!
even with Nest.SearchResponse<SegmentRecord> doesn't work
Use Moq to create the instace of Nest.ISearchResponse<SegmentRecord>(); or create a custom type, if you use Moq make sure that you didn't forget to initialize the fake object properties.
yes but it didn't work. I mocked the response as: var response = new Mock<Nest.ISearchResponse<SegmentRecord>>(); elasticClientMock.Setup(ec => ec.Search<SegmentRecord>(It.IsAny<Nest.ISearchRequest>())).Returns(response.Object); but the .Search() function keeps returning null
3

Code is from .git here: https://gist.github.com/netoisc/5d456850d79f246685fee23be2469155

var people = new List<Person>
{
    new Person { Id = 1 },
    new Person { Id = 2 },
};

var hits = new List<IHit<Person>>
{
    new Mock<IHit<Person>>().Object
};

var mockSearchResponse = new Mock<ISearchResponse<Person>>();
mockSearchResponse.Setup(x => x.Documents).Returns(people);
mockSearchResponse.Setup(x => x.Hits).Returns(hits);

var mockElasticClient = new Mock<IElasticClient>();

mockElasticClient.Setup(x => x
    .Search(It.IsAny<Func<SearchDescriptor<Person>, ISearchRequest>>()))
.Returns(mockSearchResponse.Object);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.