2017-08-02 52 views
2

如何使用sitecore fakedb創建佈局字段?我有下面的代碼,但它不起作用。如何在sitecorefakedb中創建佈局字段?

下面的代碼會引發錯誤 「的項目是不是一個佈局/渲染場」

var template = new Sitecore.FakeDb.DbTemplate("themes", Templates.SiteTheme.ID); 

       var home = new Sitecore.FakeDb.DbItem("home", new Sitecore.Data.ID());     

       var lookUpField = new Sitecore.FakeDb.DbField("link", Templates.SiteTheme.Fields.SiteTheme) { 
        Type = "Rendering" 
       }; 

       lookUpField.Value = home.ID.ToString(); 

       var site = new Sitecore.FakeDb.DbItem("site", new Sitecore.Data.ID(), Theming.Templates.SiteTheme.ID) { 
        lookUpField 
       };   

       home.Add(site); 

       db.Add(home);     

       var themesItem = db.GetItem("/sitecore/content/home/site"); 

       var layout = new LayoutField(themesItem.Fields["link"]); 

       var value = layout.Value; 

       Xunit.Assert.NotNull(value); 

回答

2

類似的東西:

[TestCase] 
public void FooLayout() 
{ 
    // arrange 
    var itemId = ID.NewID; 
    using (var db = new Db 
    { 
     new DbItem("Some Item", itemId) 
     { 
      new DbField(Sitecore.FieldIDs.LayoutField) { Value = "{presentation-xml}" } 
     } 
    }) 
    { 
     var item = db.GetItem(itemId); 
     // act 
     // assert 
     item[Sitecore.FieldIDs.LayoutField].Should().Be("{presentation-xml}"); 
    } 
} 

注:應()爲()方法來來自https://www.nuget.org/packages/FluentAssertions/