1

I am writing unit test to read from ContentSearchManager.GetIndex method. At line ContentSearchManager.GetIndex(indexableItem) in below code :

        Sitecore.Context.SetActiveSite("<<Site name>>");

        // Pull the start path of the site
        string startPath = Sitecore.Context.Site.StartPath;

        // Pull the database name
        string databaseName = Sitecore.Context.Site.Database.Name;

        // Load the web database, and get item
        var db = Sitecore.Data.Database.GetDatabase("web");

        var item = db.GetItem("<<bucket item>>");
        var indexableItem = (SitecoreIndexableItem)item;
        var index = ContentSearchManager.GetIndex(indexableItem);

I am getting error

Service Sitecore.Abstractions.ICorePipeline was not registered in container

Complete stack trace message is :

at Sitecore.ContentSearch.Abstractions.ObjectLocator.GetInstance[TService]()
   at Sitecore.ContentSearch.ContentSearchManager.GetContextIndexName(IIndexable indexable)
   at Sitecore.ContentSearch.ContentSearchManager.GetIndex(IIndexable indexable)
   at MyRadiometerTest.Services.SimpleTests.asset_that_search_working() 
   in C:\CM\Project\RadiometerUnitTest\MyRadiometerTest\Services\ContextTest.cs:line 70

Please suggest if anyone has fixed such issue or has idea about it.

Thanks, Chandana

1
  • Comments are not for extended discussion; this conversation has been moved to chat.
    – Richard Seal
    Aug 22 '19 at 13:21
1

You unit test touches static ContentSearchManager class that is shared solution wide.

Assuming you somehow mock it for this test and introduce a few ones for different conditions/aspects.

Sharing a mocked code for solution-wide tests would cause them to fail randomly on parallel execution. It would be quite hard to figure out what is wrong. It would be the main reason why to avoid any static code.

The proper way would be to wrap static code into abstractions, like Sitecore.Services.Infrastructure.Sitecore.Services.ContentSearchManagerWrapper did - introduce logical operation to locate index for IIndexable

However if it is die-or-do condition, you could look into mocking Sitecore ServiceProvider to return needed services:

Sitecore.DependencyInjection.ServiceLocator.SetServiceProvider(IServiceProvider sp)

All static code resolves dependencies from there; however you are still left with a problem of sharing test setup across tests.

Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.