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:
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!