• Not Answered

Accessing RenderingContext.Current.Rendering.Item throws exception "database value cannot be null" when unit tested

Hello,

 

We recently upgraded from 8.2 to Siecore 9.0.2 and i believe there is change in the implementation while accessing the item from Rendering in 9.0.2.

We have unit test cases set up in our solution which was working fine in 8.2.But after migration we are receiving an error "database cannot be null" when "RenderingContext.Current.Rendering.Item" was called and below is the code and stack trace 

 

RenderingContext.EnterContext(new Rendering() { DataSource = dataSource }, _context.SitecoreContext.GetItem(dataSource));

var dataSource=RenderingContext.Current.Rendering.Item;

 

I tried mocking the Sitecore Item for Rendering in my test setup but still getting the same exception.

 

Value cannot be null.
Parameter name: database

at Sitecore.Diagnostics.Assert.ArgumentNotNull(Object argument, String argumentName)
at Sitecore.Pipelines.ParseDataSource.ParseDataSourcePipeline.Run(BaseCorePipelineManager pipelineManager, Database database, String dataSource, Item relativeRootItem)
at Sitecore.Pipelines.ParseDataSource.ParseDataSourcePipeline.Run(Database database, String dataSource, Item relativeRootItem)
at Sitecore.Mvc.Data.ItemLocator.GetItemsFromDataSource(String dataSource, Item relativeRootItem)
at Sitecore.Mvc.Data.ItemLocator.GetItemsFromDataSource(String dataSource)
at Sitecore.Mvc.Presentation.Rendering.GetStaticItem()
at Sitecore.Mvc.Presentation.Rendering.get_StaticItem()
at Sitecore.Mvc.Presentation.Rendering.get_Item() 

 

Thanks,

Mahanth.M