我正在尝试使用sitecoreFakeDB创建单元测试,我的代码显示如下:
using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db() { new Sitecore.FakeDb.DbItem("source") })
{
var contextItem = db.GetItem("/sitecore/content/source");
var args = new Sitecore.Pipelines.PipelineArgs();
using (RenderingContext.EnterContext(new Rendering(), contextItem))
{
var processor = new Mock<Sitecore.FakeDb.Pipelines.IPipelineProcessor>();
db.PipelineWatcher.Register("mypipeline", processor.Object);
Sitecore.Pipelines.CorePipeline.Run("mypipeline", args);
Xunit.Assert.NotNull(args.Current));
}
}
检查RenderingContext.Current中的pagecontext时会抛出异常。如何为pagecontext设置值?
答案 0 :(得分:1)
似乎没有PageContext.EnterContext
方法类似于用于输入渲染上下文的方法。您可以通过ContextService
静态类手动设置它(但要注意可能的垃圾,如果你不能正确清理它):
ContextService.Get().Push(new PageContext());
var currentContext = RenderingContext.Current;
Assert.NotNull(currentContext.PageContext); // pass