如何从虚假网站获取 Sitecore 项目
How to get a Sitecore item from a fake site
在单元测试中我使用 Sitecore.FakeDb
。
我扩展了示例以添加带有已设置的 rootPath 的 fakeSite。
如果我尝试使用 Context.Site.GetItem(rootPath)
检索 rootItem,它 returns 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);
}
}
我错过了什么?
您需要先将假项目添加到您的假数据库中。
请在此处查看来自 github 的示例代码:
public void HowToCreateSimpleItem()
{
using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
{
new Sitecore.FakeDb.DbItem("Home") { { "Title", "Welcome!" } }
})
{
Sitecore.Data.Items.Item home = db.GetItem("/sitecore/content/home");
Xunit.Assert.Equal("Welcome!", home["Title"]);
}
}
public void HowToCreateHierarchyOfItems()
{
using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
{
new Sitecore.FakeDb.DbItem("Articles")
{
new Sitecore.FakeDb.DbItem("Getting Started"),
new Sitecore.FakeDb.DbItem("Troubleshooting")
}
})
{
Sitecore.Data.Items.Item articles =
db.GetItem("/sitecore/content/Articles");
Xunit.Assert.NotNull(articles.Children["Getting Started"]);
Xunit.Assert.NotNull(articles.Children["Troubleshooting"]);
}
}
https://github.com/sergeyshushlyapin/Sitecore.FakeDb/wiki/Creating-a-Simple-Item
https://github.com/sergeyshushlyapin/Sitecore.FakeDb/wiki/Creating-a-Hierarchy-of-Items
正如@Marek 所说,我没有创建项目,只是设置它应该指向的项目的根路径。
这是工作测试。
[Test]
public void FakeSite()
{
using (Db db = new Db("web")
{
new DbItem("NL") { { "Title", "NL Site" } }
})
{
Item siteItem = db.GetItem("/sitecore/content/NL");
// 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))
{
Assert.AreEqual("website", Sitecore.Context.Site.Name);
Assert.AreEqual("web", Sitecore.Context.Site.Database.Name);
var rootItem = Context.Site.Database.GetItem(Context.Site.RootPath);
Assert.IsNotNull(rootItem);
}
}
}
尽管我知道站点是指 CM/CD 站点。不是我要找的 MultiSite。
在单元测试中我使用 Sitecore.FakeDb
。
我扩展了示例以添加带有已设置的 rootPath 的 fakeSite。
如果我尝试使用 Context.Site.GetItem(rootPath)
检索 rootItem,它 returns 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);
}
}
我错过了什么?
您需要先将假项目添加到您的假数据库中。
请在此处查看来自 github 的示例代码:
public void HowToCreateSimpleItem()
{
using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
{
new Sitecore.FakeDb.DbItem("Home") { { "Title", "Welcome!" } }
})
{
Sitecore.Data.Items.Item home = db.GetItem("/sitecore/content/home");
Xunit.Assert.Equal("Welcome!", home["Title"]);
}
}
public void HowToCreateHierarchyOfItems()
{
using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
{
new Sitecore.FakeDb.DbItem("Articles")
{
new Sitecore.FakeDb.DbItem("Getting Started"),
new Sitecore.FakeDb.DbItem("Troubleshooting")
}
})
{
Sitecore.Data.Items.Item articles =
db.GetItem("/sitecore/content/Articles");
Xunit.Assert.NotNull(articles.Children["Getting Started"]);
Xunit.Assert.NotNull(articles.Children["Troubleshooting"]);
}
}
https://github.com/sergeyshushlyapin/Sitecore.FakeDb/wiki/Creating-a-Simple-Item
https://github.com/sergeyshushlyapin/Sitecore.FakeDb/wiki/Creating-a-Hierarchy-of-Items
正如@Marek 所说,我没有创建项目,只是设置它应该指向的项目的根路径。
这是工作测试。
[Test]
public void FakeSite()
{
using (Db db = new Db("web")
{
new DbItem("NL") { { "Title", "NL Site" } }
})
{
Item siteItem = db.GetItem("/sitecore/content/NL");
// 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))
{
Assert.AreEqual("website", Sitecore.Context.Site.Name);
Assert.AreEqual("web", Sitecore.Context.Site.Database.Name);
var rootItem = Context.Site.Database.GetItem(Context.Site.RootPath);
Assert.IsNotNull(rootItem);
}
}
}
尽管我知道站点是指 CM/CD 站点。不是我要找的 MultiSite。