1

I am trying to test one of the properties which checks for the PageMode.IsExperienceEditor. Reduced implementation of my property is given below:

public ContentResult DataSourceNotConfiguredResult
{
    get
    {
        if(Context.PageMode.IsExperienceEditor)
        {
            //Some Code
        }
        //Some other code

        return Content("Some Value");
    }
}

In my unit test, I am trying to change the display mode using the SetDisplayMode function. Here is my sample implementation for the test:

StringDictionary _stringDictionary;
FakeSiteContext _fakeSite;

[TestInitialize]
        public void Setup()
        {
            _stringDictionary = new StringDictionary();
            _stringDictionary.Add("name", "website");
            _stringDictionary.Add("database", "master");

            _fakeSite = new FakeSiteContext(_stringDictionary);
        }

[TestMethod]
        public void HiddenRenderingResult_ReturnsHtmlString_WhenCalledInExperienceEditor()
        {
            var fakeDb = new Db("master");
            Guid guid = Guid.NewGuid();
            DbItem fakeRenderingItem = new DbItem("StaticDataSourceItem", new Sitecore.Data.ID(guid));
            fakeDb.Add(fakeRenderingItem);            

            _fakeSite.SetDisplayMode(DisplayMode.Edit, DisplayModeDuration.Remember);

            using (new SiteContextSwitcher(_fakeSite))
            using (RenderingContext.EnterContext(new Rendering(), fakeDb.GetItem(new Sitecore.Data.ID(guid))))
            {
                //Do Something
            }
        }

However, even after SetDisplayMode call, I still see the DisplayMode set as normal. Here is my screenshot from the debug session:

enter image description here

I referred to this article as well which basically does the same thing.

I am confused by what I am missing. I would appreciate any pointers that nudge me in the correct direction.

Thanks!

1 Answer 1

2

It's a bug and needs to be fixed in FakeDb. But the good news is that you may hotfix it following the article instructions carefully:

  1. add shell site registration to your config:

    <sites>
        <site name="shell" domain="sitecore"></site>
    </sites>
    
  2. define class parameters in the following way (remove name, add masterDatabase):

    _stringDictionary.Add("database", "master");
    _stringDictionary.Add("enableWebEdit", "true");
    _stringDictionary.Add("masterDatabase", "master");
    

Now the test does not fail:

Assert.True(Sitecore.Context.PageMode.IsPageEditorEditing); // pass
1
  • Thanks @Serhii ! That helped! Commented Jul 31, 2019 at 13:59

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.