Created
July 22, 2015 11:23
-
-
Save kevinbrechbuehl/ac99844e1771ab3cce80 to your computer and use it in GitHub Desktop.
Sitecore Content Search Unit Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Linq.Expressions; | |
using Moq; | |
using NUnit.Framework; | |
using Sitecore.ContentSearch; | |
using Sitecore.ContentSearch.Linq; | |
using Sitecore.ContentSearch.SearchTypes; | |
namespace Website | |
{ | |
[TestFixture] | |
public class ContentSearchTest | |
{ | |
[Test] | |
public void GetProductsTest() | |
{ | |
// arrange | |
var repository = new TestableSearchRepository(); | |
// act | |
var products = repository.GetProducts(null); | |
// assert | |
Assert.AreEqual(2, products.Count()); | |
} | |
} | |
public class ProductSearchResultItem : SearchResultItem | |
{ | |
[IndexField("free")] | |
public virtual bool Free { get; set; } | |
} | |
public class SearchRepository | |
{ | |
public virtual IEnumerable<ProductSearchResultItem> GetProducts(SitecoreIndexableItem item) | |
{ | |
using (var context = this.GetSearchContext(item)) | |
{ | |
var query = context.GetQueryable<ProductSearchResultItem>(); | |
var allProducts = query.GetResults(); | |
var products = query.Filter(searchResultItem => searchResultItem.Free); | |
return products.ToList(); | |
} | |
} | |
protected virtual IProviderSearchContext GetSearchContext(SitecoreIndexableItem item) | |
{ | |
return ContentSearchManager.CreateSearchContext(item); | |
} | |
} | |
public class TestableSearchRepository : SearchRepository | |
{ | |
protected override IProviderSearchContext GetSearchContext(SitecoreIndexableItem item) | |
{ | |
// create the magic product index | |
var index = new List<ProductSearchResultItem>(); | |
index.Add(new ProductSearchResultItem { Free = true }); | |
index.Add(new ProductSearchResultItem { Free = false }); | |
index.Add(new ProductSearchResultItem { Free = true }); | |
// create the mock context | |
var context = new Mock<IProviderSearchContext>(); | |
context.Setup(c => c.GetQueryable<ProductSearchResultItem>()).Returns(index.AsQueryable()); | |
return context.Object; | |
} | |
} | |
public static class ExtensionMocks | |
{ | |
public static SearchResults<TSource> GetResults<TSource>(this IQueryable<TSource> source) | |
{ | |
return new SearchResults<TSource>(source.Select(s => new SearchHit<TSource>(0, s)), 0); | |
} | |
public static IQueryable<TSource> Filter<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate) | |
{ | |
return source.Where(predicate); | |
} | |
} | |
} |
This is really useful, but in case anyone ends up in the same situation as me, this doesn't work for queries involving facets.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When trying your solution I found the ExtensionMocks only worked when in the same assembly as GetResults is executed. This doesn't work when the unit tests are in a separate project. I have come up with an alternate solution which uses a test double for IQueryable and IQueryProvider then provides the missing Execute method that GetResults relies on.