Skip to content

Creating a Simple Item

Matt edited this page Jun 6, 2018 · 2 revisions

The code below creates a fake in-memory database with a single item Home that contains the Title field with the 'Welcome!' value (the xUnit unit testing framework is used):

[Fact]
public void HowToCreateSimpleItem()
{
  using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
    {
      new Sitecore.FakeDb.DbItem("Home") { { "Title", "Welcome!" } }
    })
  {
    Sitecore.Data.Items.Item home = db.GetItem("/sitecore/content/home");
    Xunit.Assert.Equal("Welcome!", home["Title"]);
  }
}

Important:

You should always dispose the 'db' instance properly. By default Sitecore databases are singletons, so ignoring proper test data cleaning may lead to unstable behavior in tests.

Clone this wiki locally