Git Product home page Git Product logo

Comments (2)

pveller avatar pveller commented on July 17, 2024

If the item has a template defined FakeDb will create fields on the Sitecore item using its template definition. Here's a slightly modified version of your code and it passes assertions:

    [Fact]
    public void ShouldCreateChildItemsWithFields_Alternative()
    {
      ID directoryTemplateId = ID.NewID;
      using (var db = new Db
      {
        new DbTemplate("Media folder", directoryTemplateId) { {"test1"}, {"test2"} },
        new DbItem("DownloadDir", ID.NewID, directoryTemplateId)
        {
          new DbItem("SubdirOne", ID.NewID, directoryTemplateId)
        }
      })
      {
        Item downloadDir = db.GetItem("/sitecore/content/DownloadDir");

        downloadDir.Children.Should().HaveCount(1);
        downloadDir.Fields.Should().HaveCount(2);
      }
    }

If you don't give your item a template then the fields it carries will be pushed into the Sitecore item because FakeDb will generate a template for the item based on its fields. Here's another version:

    [Fact]
    public void ShouldCreateChildItemsWithFields_AlternativeTwo()
    {
      using (var db = new Db
      {
        new DbItem("DownloadDir")
        {
          new DbItem("SubdirOne"),
          new DbField("test1"),
          new DbField("test2")
        }
      })
      {
        Item downloadDir = db.GetItem("/sitecore/content/DownloadDir");

        downloadDir.Children.Should().HaveCount(1);
        downloadDir.Fields.Should().HaveCount(2);
      }
    }

Note: prefer collection initializers to direct property setters. The code looks cleaner and FakeDb strives to provide a nice DSL for FakeDb initialization

from sitecore.fakedb.

niles11 avatar niles11 commented on July 17, 2024

Thanks for the quick response. It all sounds pretty obvious now!

from sitecore.fakedb.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.