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?

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

1 Answer 1


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);
  • 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 that you have read and understand our privacy policy and code of conduct.

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