在單元測試中,我使用的是Sitecore.FakeDb
。如何從假網站獲取Sitecore項目
我已經擴展了樣本以添加帶有rootPath的fakeSite,它被設置。 如果我嘗試使用Context.Site.GetItem(rootPath)
檢索rootItem,它將返回null。
[Test]
public void FakeSite()
{
// create a fake site context
var fakeSite = new Sitecore.FakeDb.Sites.FakeSiteContext(
new Sitecore.Collections.StringDictionary
{
{ "name", "website" }, { "database", "web" }, { "rootPath", "/sitecore/content/NL" }
});
// switch the context site
using (new Sitecore.Sites.SiteContextSwitcher(fakeSite))
{
var rootItem = Context.Site.Database.GetItem(Context.Site.RootPath); // returns null
Assert.IsNotNull(rootItem);
Assert.AreEqual("website", Sitecore.Context.Site.Name);
Assert.AreEqual("master", Sitecore.Context.Site.Database.Name);
}
}
我在想什麼?
感謝編輯@ marek-musielak –