1

I am trying to write a test for this code:

var publicationYears = new List<string>();
using (var context = searchContextBuilder.GetSearchContext(Indexes.NewsIndex))
{
    var query = context.GetQueryable<NewsArticleItem>().Where(predicate).FacetOn(n => n.Year);
    var queryResults = query.GetResults();
    ...
    publicationYears = GetYearFacets(queryResults.Facets).ToList();
}

return publicationYears;
}

The "searchContextBuilder" is injected, the "predicate" a parameter and "GetYearFacets" just a function that get the values from the facet needed.

I tried some solution (from Kevin Brechbühl) that should work, but apparently only do if the tests is in the same assembly as the code - which is not the case. So I started using the this code: https://gist.github.com/vivianroberts/1a632d8bfb8fa62e23c4679b086728ce which gave me working tests on searches without facets. In my test, I inject a "testable" version of the searchContextBuilder (using Moq):

public class TestableSearchBuilder : ISearchContextBuilder
{
    private readonly IList<NewsArticleItem> items;

    public TestableSearchBuilder()
    {
        items = new List<NewsArticleItem>();

        items.Add(...);
        items.Add(...);
        ...
    }

    public IEnumerable<NewsArticleItem> Items => items;

    public IProviderSearchContext GetSearchContext(string index)
    {
        // create the mock context
        var searchContext = new Mock<IProviderSearchContext>();
        var queryable = new SearchProviderQueryableCollection<NewsArticleItem>(items);
        searchContext.Setup(x => x.GetQueryable<NewsArticleItem>()).Returns(queryable);
        searchContext.Setup(x => x.GetQueryable<NewsArticleItem>(It.IsAny<IExecutionContext>())).Returns(queryable);
        return searchContext.Object;
    }
}

where my "SearchProviderQueryableCollection" is the "LuceneProviderQueryableStub" from the Gist.

The problem is that when using FacetOn an error occurs:

There is no method 'FacetOn' on type 'Sitecore.ContentSearch.Linq.QueryableExtensions.

Same error as Søren mentions on the page btw.. (with Filter).

I did try to get past this by putting the FacetOn also on my injected searchContextBuilder - pointing it towards Sitecores extension method in the real code and to a custom linq solution in the test. That got me a step further, but then I bumped into the call to "Facets" on the SearchResults object. In my test it was null as the facets are not passed to the SearchResults constructor. To fix that, I should include them as extra parameter in the constructor in my SearchProviderQueryableCollection:

object results = new SearchResults<TElement>(items.Select(s => new SearchHit<TElement>(0, s)), items.Count, ...);

which might be possible, but then I need to be able to construct the facets myself. And there I got stuck.. and running out of time.. and actually hoping someone already did this..

So, does anyone have an idea on how to continue getting the facets in my test - or has an alternative solution to testing queries with facets?

0

Your Answer

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

Browse other questions tagged or ask your own question.