单元测试渲染上下文。页面上下文引发异常

时间:2017-06-06 12:18:46

标签: sitecore moq

我正在尝试使用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设置值? enter image description here

1 个答案:

答案 0 :(得分:1)

似乎没有PageContext.EnterContext方法类似于用于输入渲染上下文的方法。您可以通过ContextService静态类手动设置它(但要注意可能的垃圾,如果你不能正确清理它):

ContextService.Get().Push(new PageContext());
var currentContext = RenderingContext.Current;
Assert.NotNull(currentContext.PageContext); // pass