0

I'm new to Sitecore FakeDb NUnit Testing. While testing the code, I found that

 Sitecore.Context.Item = null;
 RenderingContext.Current.Rendering.DataSource = fakeItem.ID.ToString();

Since the item is passed as Null, the corresponding data source overwrites the value.

Is this a Bug or, I'm getting it wrong here?

1
  • could you please show the (minimal) code required to reproduce the issue? Jan 12, 2018 at 6:37

1 Answer 1

5

Sitecore FakeDb does not set the context item because depending on the scenario, you might or might not need it to be set.

If you need the context item not to be null, you can set it in your test using the ContextItemSwitcher:

using (var db = new Db { new DbItem("home") })
{
    var home = db.Database.GetItem("/sitecore/content/home");
    using (new ContextItemSwitcher(home))
    {
        // Writes 'Context item = home'
        Console.Out.WriteLine("Context item = {0}", Context.Item.Name);
    }
}
1
  • Hello there, I'm following this sitecore.stackexchange.com/questions/6251/… to run a whole pipeline and success with it. However, I always have to have this "@Html.Sitecore().SwitchContextItem(myitem) " in the rendering before running the pipeline. Is it possible to use ContextItemSwitcher in the helper code so that the "@Html.Sitecore().CurrentItem" will refer to myItem. I tried and failed. Thanks! Feb 29, 2020 at 10:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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