Skip to content

AutoFixture Samples

sergeyshushlyapin edited this page Sep 27, 2016 · 18 revisions

Sitecore FakeDb provides a list of AutoFixture customizations allowing to instantiate Sitecore types.

AutoDbCustomization

A customization that enables creation of Sitecore types such as Database or Item:

[Fact]
public void CreateItemInstance()
{
  var fixture = new Fixture()
      .Customize(new AutoDbCustomization());

  var item = fixture.Create<Item>();

  Xunit.Assert.NotNull(item);
}

ContentItemCustomization

A customization that adds items to the current database. The default database is "master".

[Fact]
public void CreateContentItem()
{
  var fixture = new Fixture()
      .Customize(new AutoDbCustomization())
      .Customize(new AutoContentItemCustomization());

  var item = fixture.Create<Item>();
  var database = fixture.Create<Database>();

  var newItem = database.GetItem(item.ID);
  Xunit.Assert.NotNull(newItem);
}

Integration with unit testing frameworks

Sitecore FakeDb AutoFixtre extension does not reference any specific unit testing or mocking framework and can be used with any framework supported by AutoFixture. The code samples below demonstrate how it can be integrated with AutoFixture.Xunit.

Resolve 'master' database

Always returns the master database instance.

[Theory, AutoDbData]
public void ResolveMasterDatabaes(Database database)
{
  Xunit.Assert.NotNull(database);
  Xunit.Assert.Equal("master", database.Name);
}

Create an 'Item' instance

Creates an instance of the Sitecore.Data.Items.Item type without fields.

[Theory, AutoDbData]
public void CreateItemInstance(Item item)
{
  Xunit.Assert.NotNull(item);
}

Create a 'DbItem' instance

Creates a new Db context and a DbItem instance without fields. The instance can be added to the database.

[Theory, AutoDbData]
public void AddContentDbItem(Db db, DbItem item)
{
  db.Add(item);
  Xunit.Assert.NotNull(db.GetItem(item.ID));
}

Create an empty item in the content tree

Creates an item without fields and add it to the master database automatically.

[Theory, AutoDbData]
public void CreateContentItem([Content] Item item, Database database)
{
  var newItem = database.GetItem(item.ID);
  Xunit.Assert.NotNull(newItem);
}

Create 'DbItem' in the content tree

Creates an item without fields and add it to the master database automatically.

[Theory, AutoDbData]
public void CreateContentDbItem([Content] DbItem item, Database database)
{
  var newItem = database.GetItem(item.ID);
  Xunit.Assert.NotNull(newItem);
}

Create an item based on a custom template

In the master database, creates a root item and MyCustomTemplate that contains the Title field. The template can be used to create new items.

[Theory, AutoDbData]
public void CreateItemBasedOnCustomTemplate(
  [Content] Item root,
  [Content] MyHomeTemplate template)
{
  // act
  var home = root.Add("home", new TemplateID(template.ID));
  using (new EditContext(home))
  {
    home["Title"] = "Welcome AutoFixture!";
  }

  // assert
  Xunit.Assert.Equal("Welcome AutoFixture!", home["Title"]);
}

public class MyHomeTemplate : DbTemplate
{
  public MyHomeTemplate()
  {
    this.Add("Title");
  }
}

Getting started

Clean AutoFixture installation

Just install the Sitecore.FakeDb.AutoFixture NuGet package (it'll install the 'AutoFixture' package as well):

PM> Install-Package Sitecore.FakeDb.AutoFixture

Combination with xUnit.net (NUnit is similar)

Install the following NuGet packages:

PM> Install-Package Sitecore.FakeDb.AutoFixture
PM> Install-Package xunit
PM> Install-Package AutoFixture.Xunit2

Important:

You should use xUnit 2.x version since the version 1.x does not dispose test parameters properly which may lead to unstable behavior.

Add the following classes to your solution:

internal class AutoDbDataAttribute : AutoDataAttribute
{
  public AutoDbDataAttribute()
    : base(new Fixture().Customize(new AutoDbCustomization()))
  {
  }
}
internal class InlineAutoDbDataAttribute : InlineAutoDataAttribute
{
  public InlineAutoDbDataAttribute(params object[] values)
    : base(new AutoDbDataAttribute(), values)
  {
  }
}

Gists

Clone this wiki locally