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

1private readonly ID _renderingOptionsId = new ID(ScoreConst.TemplateIds.RenderingOptions);
2private readonly ID _viewRenderingId = ID.NewID;
3private readonly ID _folderId = ID.NewID;
4 
5private Db BuildDb()
6{
7return new Db
8{
9new DbTemplate("Folder", _folderId),
10new DbTemplate("Rendering Options", _renderingOptionsId),
11new DbTemplate("View Rendering", _viewRenderingId)
12{
13BaseIDs = new[]
14{
15_renderingOptionsId
16}
17},
18new DbItem("page", ID.NewID, new ID(ScoreConst.TemplateIds.HomePage)),
19new DbItem("renderings", ID.NewID, _folderId)
20{
21new DbItem("outside rendering", ID.NewID, _viewRenderingId),
22new DbItem("rendering folder item")
23{
24new DbItem("view rendering 1", ID.NewID, _viewRenderingId),
25new DbItem("view rendering 2", ID.NewID, _viewRenderingId),
26new DbItem("rendering folder nested", ID.NewID, _folderId)
27{
28new DbItem("view rendering 3", ID.NewID, _viewRenderingId)
29}
30}
31}
32};
33}
34 
35[Test]
36public void AddDescendentRenderings()
37{
38using (var db = BuildDb())
39{
40Item page = db.GetItem("/sitecore/content/page");
41Item folder = db.GetItem("/sitecore/content/renderings/rendering folder item");
42var context = new PlaceholderSettingsRuleContext
43{
44Item = page
45};
46 
47var action = new AllowDescendantRenderings<PlaceholderSettingsRuleContext>
48{
49ParentItemId = folder.ID.ToString()
50};
51 
52// act
53action.Apply(context);
54 
55// assert
56context.PlaceholderRenderings.Should().NotBeNull();
57context.PlaceholderRenderings.Should().HaveCount(3);
58context.PlaceholderRenderings.Should().Contain(new List<Item>
59{
60db.GetItem("/sitecore/content/renderings/rendering folder item/view rendering 1"),
61db.GetItem("/sitecore/content/renderings/rendering folder item/view rendering 2"),
62db.GetItem("/sitecore/content/renderings/rendering folder item/rendering folder nested/view rendering 3")
63});
64}
65}

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