-
Notifications
You must be signed in to change notification settings - Fork 36
AutoFixture Samples
Sitecore FakeDb provides a list of AutoFixture customizations allowing to instantiate Sitecore types.
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);
}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);
}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.
Always returns the master database instance.
[Theory, AutoDbData]
public void ResolveMasterDatabaes(Database database)
{
Xunit.Assert.NotNull(database);
Xunit.Assert.Equal("master", database.Name);
}Creates an instance of the Sitecore.Data.Items.Item type without fields.
[Theory, AutoDbData]
public void CreateItemInstance(Item item)
{
Xunit.Assert.NotNull(item);
}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));
}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);
}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);
}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");
}
}Just install the Sitecore.FakeDb.AutoFixture NuGet package (it'll install the 'AutoFixture' package as well):
PM> Install-Package Sitecore.FakeDb.AutoFixtureInstall the following NuGet packages:
PM> Install-Package Sitecore.FakeDb.AutoFixture
PM> Install-Package xunit
PM> Install-Package AutoFixture.Xunit2Important:
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)
{
}
}