-
Notifications
You must be signed in to change notification settings - Fork 36
Content Search
sergeyshushlyapin edited this page Dec 29, 2014
·
2 revisions
You can mock a Sitecore.ContentSearch.ISearchIndex
so that it returns a Sitecore.Data.Items.Item
:
[Fact]
public void HowToMockContentSearchLogic()
{
try
{
var index = Substitute.For<Sitecore.ContentSearch.ISearchIndex>();
Sitecore.ContentSearch.ContentSearchManager.SearchConfiguration.Indexes
.Add("my_index", index);
using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
{
new Sitecore.FakeDb.DbItem("home")
})
{
var searchResultItem =
Substitute.For<Sitecore.ContentSearch.SearchTypes.SearchResultItem>();
searchResultItem
.GetItem()
.Returns(db.GetItem("/sitecore/content/home"));
index
.CreateSearchContext()
.GetQueryable<Sitecore.ContentSearch.SearchTypes.SearchResultItem>()
.Returns((new[] { searchResultItem }).AsQueryable());
Sitecore.Data.Items.Item result =
index
.CreateSearchContext()
.GetQueryable<Sitecore.ContentSearch.SearchTypes.SearchResultItem>()
.Single()
.GetItem();
Xunit.Assert.Equal("/sitecore/content/home", result.Paths.FullPath);
}
}
finally
{
Sitecore.ContentSearch.ContentSearchManager.SearchConfiguration.Indexes
.Remove("my_index");
}
}