Mocking MultiSearchResponse

Hi! I was new to ES, and I am trying to mock a MultiSearchResponse, and I started from successfully mocking a SearchResponse

        var mockSearchResponse = new Mock<ISearchResponse<CandidateSearchDto>>();
        mockSearchResponse.Setup(sr => sr.Documents)
                          .Returns(new List<CandidateSearchDto>
                          {
                      new CandidateSearchDto(),
                      new CandidateSearchDto(),
                      new CandidateSearchDto()
                          });
        mockSearchResponse.Setup(sr => sr.Total)
                          .Returns(3);

        var mockMultiSearchResponse = new Mock<MultiSearchResponse>();
        mockMultiSearchResponse.Setup(ms => ms.AllResponses)
                               .Returns(new List<IResponse> { mockSearchResponse.Object });

However, on mockMultiSearchResponse it is giving me this error:

 System.NotSupportedException : Unsupported expression: ms => ms.AllResponses
Non-overridable members (here: MultiSearchResponse.get_AllResponses) may not be used in setup / verification expressions.

Is there any ways to work around this issue? I found this post

that suggests to create actual instances instead of Mock<> ones, but I am not sure how to simply initialize a MultiSearchResponse variable. Can someone help out? Thanks!!

Would anyone respond to this? Thanks!

Hi @Yuanhe_Li,

I'm not a Java expert, but maybe this can help:

Elasticsearch repository is open code, so you can peak at it to find out some best practices.

Here's search by MultiSearchResponse in the repo, page 2: Code search results · GitHub
I can see that some code there is testing code.

The testing code above uses prepareMultiSearch method: Code search results · GitHub

Looking into examples above might give you a hint, hopefully :slight_smile:

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.