I have the following code that returns Items:
public static IList<Item> GetAll(Language language)
{
var index = $"my_index_{Context.Database.Name.ToLowerInvariant()}";
using (var context = ContentSearchManager.GetIndex(index).CreateSearchContext())
{
var results = context.GetQueryable<SearchResultItem>().InLanguage(language);
return results.ToList();
}
}
I want to unit test some code that uses this function. I have set up FakeDb
to use in the test as below:
using (var db = new Db
{
new DbItem("Home") {{"Title", "Welcome!"}}
})
{
sitecoreReader.GetAllThings(); //this in turn calls GetAll(Language language)
}
Now when the code hits the line using (var context = ContentSearchManager.GetIndex(index).CreateSearchContext())
it throws this error:
Sitecore.ContentSearch.Exceptions.IndexNotFoundException
Google is not helping me on this one...
Update: using the answer below and the FakeDb documentation I was nearly able to unit test my code. I hit a wall with it when I realised my query uses extension methods that cannot be Moq'd!
The code needs some serious refactoring to be able to test!