Skip to main content

Back-End Development

Unit Testing Custom Rules, Actions, and Conditions with FakeDb – Part 2 – Testing Actions

Software Testing On A Laptop@1x.jpg

In part 2 of this 3 part series, I will cover how to test a custom action using Sitecore FakeDb.

When testing a custom action, you are testing that action alone – so I am not testing the rules engine itself, nor am I testing a specific condition (that was covered in part 1 of the series).

Sample Test

private readonly ID _renderingOptionsId = new ID(ScoreConst.TemplateIds.RenderingOptions);
private readonly ID _viewRenderingId = ID.NewID;
private readonly ID _folderId = ID.NewID;

private Db BuildDb()
{
return new Db
{
new DbTemplate("Folder", _folderId),
new DbTemplate("Rendering Options", _renderingOptionsId),
new DbTemplate("View Rendering", _viewRenderingId)
{
BaseIDs = new[]
{
_renderingOptionsId
}
},
new DbItem("page", ID.NewID, new ID(ScoreConst.TemplateIds.HomePage)),
new DbItem("renderings", ID.NewID, _folderId)
{
new DbItem("outside rendering", ID.NewID, _viewRenderingId),
new DbItem("rendering folder item")
{
new DbItem("view rendering 1", ID.NewID, _viewRenderingId),
new DbItem("view rendering 2", ID.NewID, _viewRenderingId),
new DbItem("rendering folder nested", ID.NewID, _folderId)
{
new DbItem("view rendering 3", ID.NewID, _viewRenderingId)
}
}
}
};
}

[Test]
public void AddDescendentRenderings()
{
using (var db = BuildDb())
{
Item page = db.GetItem("/sitecore/content/page");
Item folder = db.GetItem("/sitecore/content/renderings/rendering folder item");
var context = new PlaceholderSettingsRuleContext
{
Item = page
};

var action = new AllowDescendantRenderings<PlaceholderSettingsRuleContext>
{
ParentItemId = folder.ID.ToString()
};

// act
action.Apply(context);

// assert
context.PlaceholderRenderings.Should().NotBeNull();
context.PlaceholderRenderings.Should().HaveCount(3);
context.PlaceholderRenderings.Should().Contain(new List<Item>
{
db.GetItem("/sitecore/content/renderings/rendering folder item/view rendering 1"),
db.GetItem("/sitecore/content/renderings/rendering folder item/view rendering 2"),
db.GetItem("/sitecore/content/renderings/rendering folder item/rendering folder nested/view rendering 3")
});
}
}

There are a couple of things to note in this sample unit test.

First, when I create several tests of the same action, I might need to construct a structure of items using FakeDb – which in this case is extracted into a helper method called BuildDb(). Note in this example that the hierarchy created includes template inheritance which is needed for this particular test.

Second is the process to instantiate the action class using the context, and test the action. Testing the action involves creating the proper context for the rule (based on the rule type), then applying the action and testing the result by calling the action.Apply(context) method.

The act of applying the action to the context will yield some result – which can then be tested. In this example – we use fluent assertions to do so.

Thoughts on “Unit Testing Custom Rules, Actions, and Conditions with FakeDb – Part 2 – Testing Actions”

  1. Love Fluent Assertions! Thank you for a great library! We scaffold our projects with unit test samples that use Fluent Assertions and I recommend it to all devs who I introduce to unit testing with Sitecore and FakeDb.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Brian Beckham

As a Sitecore MVP, Brian spends most of his time consulting and architecting software solutions for enterprise-level Sitecore projects.

More from this Author

Follow Us