Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Damion 96 posts 331 karma points
    Jul 28, 2020 @ 09:17
    Damion
    1

    How to mock Examine search results

    I have a test where I create a mock Examine search result and test some code which should run if there are any results

    line of code I need to test:

    if (results.Any())
    {
        ...do stuff
    }
    

    And in the test:

    private Mock<ISearchResults> _mockSearchResults;
    

    in the setup:

    _mockSearchResults = new Mock<ISearchResults>();
    

    At this point the mock results do not contain anything so if statement won't be true, I'm guessing I need to add a search result item to the results but I'm not sure how to, or if that's even correct. Here's what I was thinking, but it gives the error 'Invalid setup on an extension method' :

    _mockSearchResults.Setup(x => x.Append(_mockSearchResult.Object)).Returns( _mockSearchResults.Object );
    
  • Paul Taylor 13 posts 100 karma points
    Jan 30, 2024 @ 14:31
    Paul Taylor
    0

    Rather than mock your search results, you can fake them like this:

        private ISearchResults GetSearchResults()
        {
            var fakeResults = new List<ISearchResult>();
            for (var idx = 0; idx < 5; idx++)
            {
                var result = new SearchResult(idx.ToString(), idx, () => { return new Dictionary<string, List<string>>(); });
                fakeResults.Add(result);
            }
            return new LuceneSearchResults(fakeResults, fakeResults.Count);
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft