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?

  • could you please show the (minimal) code required to reproduce the issue? – Serhii Shushliapin Jan 12 '18 at 6:37
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);
    }
}

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.