2017-06-06 23 views
0

我想用我的sitecoreFakeDB代码创建单元测试下方显示:在它抛出一个异常的RenderingContext.Current检查的pageContext单元测试渲染上下文。页面的上下文抛出一个异常

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)); 
      } 
     } 

。我怎样才能为pagecontext获得一个值? enter image description here

回答

1

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

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