Kyle Kingsbury A Sitecore web development blog!

Unit-testing devices and layouts with Sitecore.FakeDb

U

After writing a few extension methods that checked whether a Sitecore Item had a layout for the default device or any device, I wondered if I could use the Sitecore.FakeDb library to unit-test these extension methods and provide some much needed code coverage for a project i’m working on. As most people do, I quickly did a few google searches and I was shocked, I couldn’t find any example’s showing what is needed to setup Sitecore.FakeDb to test against devices and layouts.

After some trial and error, I finally came across some useful tidbits that you can use in order to setup Sitecore.FakeDb to test Item’s with devices and layouts.

For the sake of this post, i’ll be using the following extension method:

I’ll break these tidbits into three areas:

  • Scaffolding – setting up the database for Sitecore’s API’s
  • Presentation – adding presentation to an Item
  • Testing – getting and testing against our Item 

Initial Sitecore Scaffolding

Unfortunately, as I quickly learned, we aren’t able to apply XML to the layout field and expect things to magically work. Sitecore uses a series of classes (ItemVisualizations, Devices, etc) after parsing the XML that will query specific roots for all devices and layouts and validate that ID’s referenced in the XML actually exist. Luckily, Sitecore makes it rather easy to use the same ID’s and TemplateID’s by exposing two classes: ItemIDs and TemplateIDs.

In addition to creating the scaffolding for Sitecore, we have added a default device and a default layout that we can now reference in our XML.

Adding Presentation Values

Next up, we need to manually create the XML that will be used for the presentation values of our item. As you can see below, a neat trick I learned from reading the source code of Sitecore.FakeDb is Sitecore has exposed a few helper methods from the XmlDeltas class that we can leverage to create valid XML for the layout field by mocking the XML generated on the standard values and applying a delta (as if we were editing an instance of the item).

Getting and testing against our Sitecore Item

The last thing we need to do before writing a valid Unit-Test is to use the Sitecore API to get the Item to perform our unit-tests against. With the above setup and the FluentAssertions library, our Unit-Test becomes a single-line statement.

Testing a Sitecore Item with a blank layout

While the setup took a few tries to get Sitecore to play nice, I was pleasantly surprised how easy it was to test my Item. I hope you enjoyed this post! Let me know in the comments your tidbits for testing Sitecore’s devices and layouts.

Other libraries used: FluentAssertions, AutoFixtureSitecore.FakeDb.AutoFixture

Add comment

Kyle Kingsbury A Sitecore web development blog!

Recent Posts

Tags